Rename files and folders with git
As easy as it sounds, it turned out renaming files and folders with git can sometimes be pretty painful. So it happened to me that I was working on a branch of a project and had to rename a subfolder.
Renaming with git mv
For renaming files or folders use nothing but the git mv command. git mv takes at least two arguments, a source and a destination. If you want to move several files to a single path you may specify n sources but the last argument is the destination.
Here’s what ‘git mv’ actually does:
mv oldfolder newfolder git add newfolder git remove oldfolder
It performs a file move, adds the new file to the index and removes the old file from it. We can see there’s no commit so we have to add the updates and commit the changes afterwards.
How to use
Assuming you’d like to change a folder’s name from oldfolder to newfolder
git mv oldfolder newfolder
If there’s already a newfolder in your repo and you’d like to overwrite it use –force
git mv -f oldfolder newfolder
Do not forget: you have to add the changes to the index and commit them after renaming with git mv.
git add -u newfolder git commit -m "changed the foldername whaddup"
The -u option at the add command is important here, it’ll update already tracked files/folders.
This is a pretty simple usecase and should work almost every time. Except, and here’s the part where it gets interesting:
Renaming foldername to folderName on case insensitive file systems
Why is this more interesting? Because simple renaming with a normal mv command(not git mv) won’t get recognized as a filechange from git. If you try it with the ‘git mv’ command like in the following line
git mv foldername folderName
If you’re using a case insensitive filesystem, e.g. you’re on a Mac and you didn’t configure it to be case sensitive, you’ll experience an error message like this one:
fatal: renaming ‘foldername’ failed: Invalid argument
And here is what you can do in order to make it work:
git mv foldername tempname && git mv tempname folderName
This splits up the renaming process by renaming the folder at first to a completely different foldername. After renaming it to the different foldername the folder can finally be renamed to the new folderName. After those ‘git mv’s, again, do not forget to add and commit the changes. Though this is probably not a beautiful technique, it works perfectly fine. The filesystem will still not recognize a change of the letter cases, but git does due to renaming it to a new foldername, and that’s all we wanted
Other interesting observations
git mv has a command option called ‘–dry-run’ (short: ‘-n’) which is supposed to show what changes will be made if you run the command, but it won’t execute the command. Funny thing is that if you rename a folder/file with the -n option:
git mv -n foldername folderName
it will show the changes that should happen (even on a case insensitive filesystem) so you think everything should work fine but running it without -n will then fail on case insensitive filesystems.
Okay, honestly.. it was not that painful, but for a simple rename it was a lot of debugging time I’ve spent on it.