Search code examples
gitgitlab

git stash or git add doesn't work on branch for particular file after folder rename on MacOS


Backstory: There was one frontend repo(let's call it repo A) in my organization which had casing issues in names of folders, I renamed a folder to confirm it with casing of other folders. During this adventure I noticed that git doesn't support folder rename, I looked up on stackoverflow for help and somehow was able to rename the folder, then commit some changes into the files of that particular folder.

Present: I cloned repo A and switched to the branch in which I had did my folder-rename adventure. I notice that there are some changes already in staging area, ready to be git add I did git add curious that I had already done it before in my commits, I did git status again and to my shock, the files were still there, I tried git stash and observed that there was an entry in git stash but there were still 3 files in staging area, ready to be git add, I did git stash again but there were still 3 files.

Note: I just observed that in remote my folder name is metricCharts but when I clone it in my system folder name stays MetricCharts and when I git stash it, I see that folder name in path changes to metricCharts and then if I try git add, it is added

My Question is why is this happening?


Solution

  • The default file systems on macOS and Windows are case insensitive on search. They store the file name using the case that you provided but they find metricCharts both when you search for metricCharts or MetricCharts. This fouls Git when a file is renamed and the only difference is in casing.

    The solution is very simple and it involves two renames:

    1. First of all, make sure that you don't have uncommitted changes in the working directory (git status must report "nothing to commit, working tree clean").
    2. Rename the file on disk but make its name different than the original name, not only in casing. For example, add one letter or a digit at the end. Add the changes to Git. There is no need to commit yet.
    3. Rename the file on disk again and this time use the desired final name. Add the changes to Git and commit

    That's all. It works for directories the same way.