Git – Renaming or moving files

The best way to move or rename a file is doing it a via git’s mv command, e.g.:

$ git mv filename.txt filename2.txt

This will not only make git aware that the file has been moved (or in this case renamed), but it will actually rename the file in the working directory.

You can still use the traditional mv command, but this won’t get picked up git, and so you have to manually tell git of this, like this:

mv filename.txt filename2.txt
git rm filename.txt
git add filename2.txt

Hence the git mv command can do all of the above, but in a single line.

Finally you have to commit the change to take a snapshot for the above to be captured in git’s internal db:

git commit -m "filename.txt has been renamed to filename2.txt"