
The most basic way to use Git is to use the git clone command to clone an origin Git repository (such as example.git) to a directory on your PC (such as /home/john.doe/git), make a change to a file in the cloned repository on your PC (such as example.txt), use the git commit command to commit the change to the file, and to then use the git push command to upload the file to the origin Git repository.
Branches are used as an isolated way to make changes to files in a repository. A common example would be to create a new branch using the git branch or git checkout command, switch to the new branch using the git checkout command, make a change to a file, commit the change using the git commit command, and then merge the branch to the master branch using the git merge command.
The git mv command can be used to:
- Move a file from one directory to another directory
- Rename a file
AVOID TROUBLE
The git mv command should always be used instead of the Linux mv command.
Move a file from one directory to another directory
Let's say you have a file in the currently selected branch of the cloned repository and you want to move the file to some other directory of the cloned repository. In this example, we want to move the sample.txt file from the foo directory to the bar directory.
git mv foo/sample.txt bar/
The git status command will show that the file has been renamed (technically, moved).
~]$ git status
On branch fetch_inventory_from_master_servers
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: foo/sample.txt -> bar/sample.txt
Let's commit the change. Notice that we reference the original location of the file, not the new location of the file.
git commit -m "moved from the foo directory to the bar directory" foo/sample.txt
Once you commit the change, the git status command will show that the file has been deleted from the old location.
deleted: foo/sample.txt
Rename a file
Let's say you want to rename foo.txt in the currently selected branch of the cloned repository to bar.txt. You can use the -n or --dry-run flag to see what should happen. In this example, no issues are detected, meaning the file should be renamed to bar.txt.
~]# git mv foo.txt bar.txt --dry-run
Checking rename of 'foo.txt' to 'bar.txt'
Renaming foo.txt to bar.txt
Use the git mv command without the -n or --dry-run flags to actually rename the file. If the rename is successful, no output should be displayed, and the file should have been renamed to bar.txt.
git mv foo.txt bar.txt
The git status command should show that foo.txt has been renamed to bar.txt.
~]# git status foo.txt
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: foo.txt -> bar.tst
You can now commit the change to bar.txt.
~]# git commit -m "renamed foo.txt to bar.txt" bar.txt
1 file changed, 208 insertions(+)
create mode 100755 bar.txt
Now the git status command should show that foo.txt has been deleted.
~]# git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: foo.txt
You can now commit the change to foo.txt.
~]# git commit -m "renamed foo.txt to bar.txt" foo.txt
1 file changed, 208 insertions(+)
delete mode 100644 foo.txt
And now the git status command should show that the working tree is clean.
]$ git status
On branch main
nothing to commit, working tree clean
If the files are not in HEAD, you can merge the file in your currently selected branch to the main branch and then push HEAD to the origin Git repository.
git push origin HEAD
Did you find this article helpful?
If so, consider buying me a coffee over at