Script to stage file-path case changes in a Git repository.
Support this project by ⭐️ starring and sharing it. Follow me to see what other cool projects I'm working on! ❤️
After renaming files, run the script with npx in your Git repository:
npx git-detect-case-change
If there were any case-changes, it will detect and stage them for you.
Run with --dry
to see what files would be renamed before staging them:
npx git-detect-case-change --dry
Pass in specific paths after --
to scope the search to:
npx git-detect-case-change -- <scope to directory path>
File-systems on macOS & Windows are case-insensitive by default, which means paths /a.txt
and /A.txt
cannot exist at the same time. Because of this default, Git is also case-insensitive by default, preventing it from detecting case changes in file names.
The recommended solution
from this StackOverflow discussion is to rename the files individually with git mv
:
git mv <old-path> <new-path>
However, this may not be practical if the case-changes were made without Git (eg. automated by another program) and there's a lot to rename.
This script automates case-change detection for Git.
-
Get the case-sensitive file paths from the current Git project:
git ls-tree --name-only -r HEAD
-
Check each file path with
fs.promises.exists
to find a case-insensitive match. -
If the path exists with a different case, register the change with Git:
git mv <old-path> <new-path>