# How do I rename a file in git that differs by case only?

In my git (Xcode) project, I committed a filename with improper case. Now I went to try to rename it, but when I commit, I get:

fatal: Will not add file alias 'MyProject/MyFile.h' ('MyProject/MyFilE.h' already exists in index)

I'm sure this is because git isn't used to be running on a case-preserving, case-insensitive filesystem like HFS+. But how do I get around it?

I tried renaming both files to something else, and committed that, and then renaming them back to what I want, but it still fails (with the same error message).

As asked and answered on stackoverflow, there's an easier way to do this that doesn't involve making a new file system in a disk image file:

Move your existing file aside, commit that move, then move it back using the case you'd like it to maintain and commit. Done.

Example:

mv foo foo2
git commit -m "renaming"
mv foo2 FOO
git commit --amend -m "renamed foo to FOO"

• in the interests of avoiding link rot we usually ask you copy the relevant portions of the answer behind the link in to the body of the answer. – Ian C. May 18 '12 at 15:11
• Thanks @ian-c. I was hoping that this rule does not apply to links inside the stackexchange family. – mspasov May 18 '12 at 15:14
• Yea, I get what you're saying but consistency is nice. – Ian C. May 18 '12 at 15:15
• Weird. I was positive I tried this, but today it works. Thanks. – Ken May 20 '12 at 14:04

I have not yet found a way to fix this directly on a case insensitive file system, you need a case sensitive filesystem.

Luckily Mac OS X has good virtual disk mounting support, so you can quickly & easily get (make) a (temporary) case sensitive filesystem on your computer.

Open up Disk Utility, make a New Image and set the "Format" field to one of the case-sensitive versions of "Mac OS Extended". Also make sure you make it big enough for your repository.

Mount your new disk, check out the git repository onto it, make sure the correctly named file has the correct contents, and delete the other one. Commit and push your changes, and then you can unmount the disk image and delete it.

If you don't have a remote server to clone from (i.e. you're just running a git repo locally on your hard drive), remember you can clone directly from the other git repository on your hard drive.

• That's awfully labour intensive. You can do this without having to spin up a new file system in a disk image file. – Ian C. May 18 '12 at 15:08

Step 1: Just rename the file with a name that is more different (than only the case of letters.) Just pick a random name different from the current name and from the name you want to give it.

Step 2: commit this file by GUI (Xcode) or by CLI (Terminal)

Step 3: rename the file as you wanted to rename it in the first place. So now, there will also be more difference than just the case with the previous name.

Step 4: commit the renamed file again.

No magic tricks needed. And you can choose to commit by GUI or CLI.

I use sourcetree. Without using CLI there is easy step to do this. rename file to another name(for example Index.php to Index_old.php) then commit and push .. again change the name to desired name (index.php) then commit and submit.