Git stores files as objects for every commit. In some cases, you may need to change the name of single or multiple files. Git provides different ways to rename a file. After renaming the file it will be related to the previous name and will continue tracking the file. The rename operation will be stored in the change history.
git mv Command
Git provides the
git mv command in order to move files and folders. But as you know moving a file is the same as renaming a file in the same or different path. The syntax of the “git mv” command is like below which can be used to rename files.
git mv OLD_FILE NEW_FILE
- OLD_FILE is the original file name which will be changed to NEW_FILE.
- NEW_FILE is the new file name.
In the following example, we rename the file
$ git mv hw.c hardware.c
The rename can be checked with the
git status command like below.
$ git status
Force Renaming Even Same Name File Exist
In some cases, the new name can be already used by another file. This will prevent rename operation and the file stays with its old name. But we can force the rename and overwrite the existing file with the
$ git mv -f hw.c hardware.c
Alternatively the long form of the
-f option can be used which is
$ git mv --force hw.c hardware.c