In Git, files and folders are the essential components of the Git repository. Each Git repository contains single or multiple files and folders. Git permits users to create, modify, delete, move, and rename files and folders in the repository. Moreover, users can also move specific files to a particular folder.
This post will illustrate:
- What is the “git mv” Command in Git?
- How to Move a File Using the “git mv” Command?
- How to Rename a File Using the “git mv” Command?
What is the “git mv” Command in Git?
In Git, the “git mv” command is a convenient way to move or rename a file or directory in a Git repository. It automatically updates the index after the operation, so that the change can be committed with a simple “git commit” command.
Syntax
The syntax of the “git mv” command is:
Here:
- “<source>” is the name of the file or directory you want to rename or move.
- “<destination>” is the new name or path of the file or directory.
How to Move a File Using the “git mv” Command?
To move a file to a particular folder, check out the below-provided instructions:
- Navigate to the local repository.
- Select the desired file.
- Move the file to the specific folder using the “git mv <file-name> <folder-name/path>” command.
- Verification.
Step 1: Move to Local Repository
First, redirect to the specific local repository using provided command:
Step 2: View Repository Content
Then, list the content of the current repository and choose a desired file that needs to be moved to the particular folder:
The below output displays the files and folder of the current repository. We have selected the “File1.txt” file and we will move it to the “Folder1” folder:
Step 3: Move File to Folder
Now, utilize the “git mv <file-name> <folder-name/path>” command to move the selected file to the desired folder:
Here:
- “txt” is the file name that will be moved.
- “Folder1” is the folder where the file is moved:
Step 4: Verification
To verify whether the “File1.txt” file is moved to the “Folder1” folder or not, list the repository content:
In the below output, the “File1.txt” file cannot be seen which indicates that it has been moved to the “Folder1” folder:
Now, use the provided command and navigate to the “Folder1” folder:
Finally, list the folder content to view the “File1.txt” file:
In the below screenshot, the “File1.txt” file can be seen:
How to Rename a File Using the “git mv” Command?
To rename a file in Git, follow the provided steps:
- Choose a particular file.
- Rename the file using the “git mv <old-file-name> <new-file-name>” command.
- Verification.
Step 1: Select a Specific File
First, display the repository content, and choose the particular file that needs to be renamed:
The below output shows all the files of the repository. We have selected the “myFile.txt” file to rename it:
Step 2: Rename the File
Then, rename the selected file with the new name using the “git mv <old-file-name> <new-file-name>” command:
Here:
- “txt” is the old name of the file.
- “txt” is the new name of the file:
Step 3: Verification
To ensure that the desired file has been renamed successfully or not, list the repository content:
It can be seen that the “myFile.txt” file has been renamed to the “Feat.txt” successfully:
That was all about the “git mv” command and its usage in Git.
Conclusion
The “git mv” command in Git is a convenient way to move or rename a file or directory in a Git repository. To move a specific file to the desired folder, the “git mv <file-name> <folder-name/path>” command is used. Moreover, utilize the “git mv <old-file-name> <new-file-name>” command to rename the particular file with the new name. This post has illustrated about “git mv” command in Git and its uses.