Friday, May 15, 2015

Resolving GIT Windows/Mac case sensitive file names problems using sparse-tree

Linux OS allows you to add files with same name but with varied case in a same folder.
For example:

  • In the directory: /home/siddesh/git-area/myproj/dir1
  • I can add 2 files: fileName FileName
  • As you can see, the name of these files are same, but the capitilization is different
You can add these files into Git repository from Linux OS.

But when you clone this GIT repo on Windows, you'll get problems
----------------------------------

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   myproj/dir1/fileName
-----------------------------------------

Though you have not modified this file, it showing as modified. Because windows is case insensitive and it can't keep to files with the same name in the same folder
  • i.e. it can't keep  2 files: "fileName" & "FileName" in same folder.
You can't un-modify these changes
  • git checkout -- myproj/dir1/fileName
  • It doesn't help. Still it shows as modified
Even git doesn't allow you to switch branch peacefully.
---------------------------------------------------
git checkout -b newbranch origin/newbranch

error: Your local changes to the following files would be overwritten by checkout:
      modified:   myproj/dir1/fileName
Please, commit your changes or stash them before you can switch branches.
Aborting
-----------------------------------------------------------

You can't stash them as well. But you can forcefully switch the branch using "git checkout -f", but these modified files will carry forward to new branch to disturb your peace.

What is the fix?
  • You need to rename these files on a git repo checked out in Linux, commit and push it to repo
Any other solution until it is renamed?
You can use git "sparse-tree" feature, which comes in git version 1.7 & above. You can ask git to ignore this problematic folder from working area. 

  • git config core.sparsecheckout true
  • vi .git/info/sparse-checkout
---------------------------------------------

!myproj/dir1/*
/*
--------------------------------------------
"!myproj/dir1/*" : It means ignore myproj/dir1/*
/* :   It means add everything else

  • git read-tree --reset -u HEAD