Change case of a file on Windows?

WindowsGitFilenames

Windows Problem Overview


There are a couple of files in our git-controlled codebase that I'd like to rename. Specifically, I just want to change the case of the file, so that sourceCode.java becomes SourceCode.java, for example. The catch: I'm on a Windows box, and the filesystem thinks those are the same file name.

How can I get Windows and Git to recognize that change and check it in? The change of the file name should not be ignored, but committed to git.

Windows Solutions


Solution 1 - Windows

To rename the file you can use the standard git mv command. Since Windows treats files with only changes in case as identical, you have to pass the -f option to force a rename:

git mv -f name.java Name.java

If instead you want to ignore case changes, have a look at the question https://stackoverflow.com/questions/52950/how-to-make-git-ignore-changes-in-case.

Solution 2 - Windows

If you are on a FAT file system your only choice is to do a two stage rename:

  1. Rename sourceCode.java to anything.you.like
  2. Rename anything.you.like to SourceCode.java

Back in the days when we used Perforce we had exactly this problem and this was the only solution we could come up with.

Solution 3 - Windows

The following steps allowed me to change the case on Windows:

  • Add ignorecase = false to [core] in .git/config;
  • Move the files you are going to rename out of your project directory;
  • Add the deletes to the index;
  • Move all files back to their original location and change the case of the files and/or directories;
  • Add all "new" files to the index;
  • Remove ignorecase = false added at the first step.

This way you have a single commit that contains the rename and it makes it easy to change e.g. an entire directory.

Solution 4 - Windows

In my opinion one simple way is missing. You can do this for a single file, a specific directory or even the whole repository. Just rename your files in any way you like before and than execute these commands:

git rm --cached <file name or directory>
git add <file name or directory>

If you want to affect also the sub-directories, you have to use the -r flag:

git rm -r --cached <directory>
git add <directory>

Solution 5 - Windows

Be careful. Doing this can lead to changes that are impossible to merge. Git gets confused when merging on Windows because it can't decide whether the old Uppercase name and the new lowercase name are the same file or not (for Git they are not, but for the filesystem they are). To merge you have to do some manual workaround like deleting the files before merging.

See https://stackoverflow.com/questions/8604535/git-rebase-issue-with-files-of-same-name-but-different-case

I'm not sure if this issue is worse than having an unconventionally-named file in your project for ever and ever, but it is worth knowing about if there are lots of users with lots of branches which will all need to be merged eventually.

Solution 6 - Windows

With NTFS (or FAT), a single git mv command does not solve the problem. This question shows a technique that works: https://stackoverflow.com/questions/3011625/git-mv-and-only-change-case-of-directory

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionElectrons_AhoyView Question on Stackoverflow
Solution 1 - WindowsIgor ZevakaView Answer on Stackoverflow
Solution 2 - WindowsChrisFView Answer on Stackoverflow
Solution 3 - WindowsPieter van GinkelView Answer on Stackoverflow
Solution 4 - WindowsNils-o-matView Answer on Stackoverflow
Solution 5 - WindowsjwgView Answer on Stackoverflow
Solution 6 - WindowsRainer BlomeView Answer on Stackoverflow