Git case gotcha on os x

Sat 10 March 2012

tags: git osx

Found myself caught by a gotcha using git on os x today. The issue is the way os x (hfs+) handles letter case in file names. File names are case insensitive, which means that File and file are considered to be the same file.

To see this in action, create a new repo, touch a file named File, add it and commit. Then try renaming it to file, and check git status.

$ git init         # create a new repo
$ touch File       # create a file
$ git add File     # add it to the index

Current status:

$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   File
#
$ git commit -m "add File"
[master (root-commit) 2c249b7] add File
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 File

Now, we try to rename File to file:

$ ls
File
$ mv File file
$ ls
file
$ git status
# On branch master
nothing to commit (working directory clean)

Huh? Turns out, git doesn't notice the change. Ok, let's try moving back, and explicitly telling git what we want:

$ mv file File
$ git mv File file
fatal: destination exists, source=File, destination=file

No joy. In fact, the best i could come up with is the rather awkward:

$ git mv File File.tmp
$ git mv File.tmp file
$ ls
file
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       renamed:    File -> file
#

Finally, git's doing what I want. A slightly silly dance required, but at least it works. Better ideas on how to do this are most welcome.

Comments !