I have changed a few files name by de-capitalize the first letter, as in Name.jpg
to name.jpg
. Git does not recognize this changes and I had to delete the files and upload them again. Is there a way that Git can be case-sensitive when checking for changes in file names? I have not made any changes to the file itself.
24 Answers
As long as you're just renaming a file, and not a folder, you can just use git mv:
git mv -f yOuRfIlEnAmE yourfilename
(As of a change in Git 2.0.1, the -f
flag in the incantation above is superfluous, but it was needed in older Git versions.)
-
13this gives me 'source directory is empty' while it is not Commented Jul 19, 2014 at 13:10
-
14Using MacOS here (case-insensitive FS) and -f worked! Thanks for the tip Commented Dec 15, 2016 at 18:08
-
23Don't forget to give the full file path. Obvious, I know, but got me for a while Commented Aug 15, 2017 at 16:03
-
20To the top voted comment: you do need the
-f
switch with the latest git (2.18) otherwise you could get thefatal: destination exists
error. Commented Feb 15, 2019 at 17:53 -
19It's important to note that this doesn't work if you're trying to rename a directory instead of a file. If you need to rename a directory, you have to do a
git mv
for each file in the directory instead. Commented Nov 18, 2020 at 18:01
Git has a configuration setting that tells it whether to expect a case-sensitive or insensitive file system: core.ignorecase
. To tell Git to be case-sensitive, simply set this setting to false
. (Be careful if you have already pushed the files, then you should first move them given the other answers).
git config core.ignorecase false
Note that setting this option to false on a case-insensitive file system is generally a bad idea. Doing so will lead to weird errors. For example, renaming a file in a way that only changes letter case will cause git to report spurious conflicts or create duplicate files(from Mark Amery's comment).
Documentation
From the git config
documentation:
core.ignorecase
If true, this option enables various workarounds to enable git to work better on filesystems that are not case sensitive, like FAT. For example, if a directory listing finds
makefile
when git expectsMakefile
, git will assume it is really the same file, and continue to remember it asMakefile
.The default is false, except git-clone(1) or git-init(1) will probe and set
core.ignorecase
true if appropriate when the repository is created.
Case-insensitive file-systems
The two most popular operating systems that have case-insensitive file systems that I know of are
- Windows
- OS X
-
20On a side note, I don't think that Mac OS X itself is case-insensitive. Instead, it's the filesystem that determines case-sensitivity. When formatting a HFS+ partition, users can choose whether to make it case-sensitive or insensitive. Case case-insensitive is the default. Commented Dec 16, 2014 at 19:02
-
340It seems very worth noting in this answer that setting this option to
false
on a case-insensitive file system is a bad idea. This isn't necessarily obvious. For example, I just tried this on my Mac, thinking it would fix my problems, then renamed a file fromproductPageCtrl.js
toProductPageCtrl.js
.git status
saw a new file calledProductPageCtrl.js
but didn't think thatproductPageCtrl.js
had been deleted. When I added the new files, committed, and pushed to GitHub, the GitHub repo now contained both files even though my (supposedly up to date) local repo had only one. Commented Feb 9, 2015 at 10:25 -
8@MarkAmery That sounds a lot like a bug in your Git client. Did you file a report?– DomiCommented Feb 13, 2015 at 11:49
-
46@Domi this is not a bug, this is expected behavior. It is in fact a bad idea to set this to false on an insensitive filesystem because this is what happens. The reason why git didn't saw the lower-case file been deleted is that the filesystem doesn't report it as deleted as it does ignore the case while git does not with this option set to false. Its not that the filenames don't have lower vs upper case on ntfs or fat its just that filename lookup is ignoring the case.– ohcibiCommented Jun 2, 2016 at 17:40
-
34@Domi git is smart enough. Thats why you should not set this to false on a case insensitive file system. Use
git mv
to move the file and see how git manages it. If you move the file without git, there is nothing git can do as the filesystem isn't telling the truth to git. This is an issue of ntfs/fat/hfs and thelike and not git/linux.– ohcibiCommented Jun 3, 2016 at 5:06
Fix git filename case on whole repo:
(replace .
with (sub-)folder name for just part of the repo)
git rm -r --cached .
git add --all .
git status ##Review that **only** changes staged are renames
## Commit your changes after reviewing:
git commit -a -m "Fixing file name casing"
git push origin main
Explanation from @Uriahs Victor comment:
What this command actually does is deletes the cached version of the file/folder names that git thought still existed. So it will clear its cache but leave everything in the current folder (where you've made your changes locally) but it will see that those other wrong case folders/files do not exist anymore so will show them as deleted in git status. Then you can push up to GitHub and it will remove the folders/files with wrong cases. This answer has a graphic depicting what the command means.
Caveats
- This removes files that were
.gitignore
'd, but force-added inside an ignored folder, e.g.git add -f ignored/folder/force-added.file
. Simply revert the deletion withgit checkout HEAD -- ignored/folder/force-added.file
- This removes explicit git file permissions (
git update-index --chmod=...
) from files, e.g.gradlew
. Either re-apply the permissions manually until the file is no longer showing as "modified" ingit status
; or reset the file to what's in the repo:git checkout HEAD -- path/to/file/with/executable-bit.set
- For submodules we can get "You've added another git repository inside your current repository." warning, in that case do what it says:
git rm --cached submodule/path git submodule add <url-of-submodule-remote> submodule/path
-
47This is the solution. And unlike the other answers it works well when you are doing batch renames. On glamourphilly.org we needed to change every .Jpg to .jpg. In Finder you can do batch renames like that and this answer lets you check it in. Commented Dec 9, 2019 at 22:50
-
3
-
1This is the easiest solution by far. Works for files and directories. Thanks!– GazCommented Sep 28, 2021 at 18:50
-
12What this command actually does is deletes the cached version of the file/folder names that git thought still existed. So it will clear its cache but leave everything in the current folder (where you've made your changes locally) but it will see that those other wrong case folders/files do not exist anymore so will show them as deleted in
git status
. Then you can push up to github and it will remove the folders/files with wrong cases. This answer has a graphic depicting what the command means: stackoverflow.com/a/41863575/4484799 Commented Oct 3, 2021 at 22:24 -
2
Using SourceTree I was able to do this all from the UI
- Rename
FILE.ext
towhatever.ext
- Add/Stage that file
- Now rename
whatever.ext
tofile.ext
- Add/Stage that file again
It's a bit tedious, but if you only need to do it to a few files it's pretty quick
-
5
-
11"Stage that file" is the important part - none of the other answers above worked for me. It actually worked with the plain old Windows command prompt. Commented Dec 21, 2017 at 16:43
-
3I didn't realize this worked through the staging area. But in my case, I wanted to modify the folder names as well as some files within those folders. So I first renamed all folders to temporary names. Committed the new names (all files within) and the "deleted" files. Git flagged them all as "renamed". Then renamed all those folders back to their new case versions and committed again. Finally, merged those 2 commits. But based on what you wrote, I could have done the whole thing through the sating area directly, without creating 2 commits + merge. Commented Oct 12, 2018 at 16:08
-
4
-
2This must be the simplest and easiest solution to understand suggested here. For git-noobs (like me) to "Stage" a file means to "add" a file without committing file.– JoelCommented Feb 9, 2022 at 9:39
This is what I did on OS X:
git mv File file.tmp
git mv file.tmp file
Two steps because otherwise I got a “file exists” error. Perhaps it can be done in one step by adding --cached
or such.
-
27as the top answer suggests,
-f
(force) is the flag you are looking for– rperryngCommented Jul 9, 2015 at 22:08 -
8@rperryng - no, the
-f
flag doesn't help in case the underlying FS is case-insensitive. However, two-step solution worked for me– HEKTOCommented Sep 28, 2016 at 18:34 -
Using a case-insensitive FS (on Mac) and
-f
worked! Thanks for the tip Commented Dec 15, 2016 at 18:07 -
-
3
git -c "core.ignorecase=false" add .
will consider files whose case has been changed for commit. Commented Nov 11, 2017 at 23:24
Sometimes it is useful to temporarily change Git's case sensitivity.
Method #1 - Change case sensitivity for a single command:
git -c core.ignorecase=true checkout mybranch
to turn off case-sensitivity for a single checkout
command. Or more generally: git -c core.ignorecase=
<<true or false>>
<<command>>
. (Credit to VonC for suggesting this in the comments.)
Method #2 - Change case sensitivity for multiple commands:
To change the setting for longer (e.g. if multiple commands need to be run before changing it back):
git config core.ignorecase
(this returns the current setting, e.g.false
).git config core.ignorecase
<<true or false>>
- set the desired new setting.- ...Run multiple other commands...
git config core.ignorecase
<<false or true>>
- set config value back to its previous setting.
-
1Why not directly
git -c core.ignorecase=<true or false> checkout <<branch>>
? Nothing to reset after.– VonCCommented Jul 10, 2018 at 11:28 -
2I had a weird experience of the proposed core.ignorecase working when changing from lowercase to uppercase, but not for uppercase to lowercase. seems the only reliable solution is to stop using an OS which fails to recognise filename case. Commented Sep 30, 2018 at 2:56
-
Is there a reason why it should be a temporary change? Would that cause any problem if I just leave the settings changed to case sensitive?– cytsunnyCommented Jan 24, 2019 at 9:39
-
This may depend on a few factors, in particular whether the target file system is case sensitive - see en.wikipedia.org/wiki/Case_sensitivity#In_filesystems. The temporary change may be needed if the deployment file system has different case sensitivity to the file system used for development. Also in my case I work in a team where everyone is expected to have the same Git settings (i.e. case sensitive) so if I turn it off it needs to be temporary. Commented Jan 24, 2019 at 9:49
We can use git mv command. Example below , if we renamed file abcDEF.js to abcdef.js then we can run the following command from terminal
git mv -f .\abcDEF.js .\abcdef.js
-
3Forcing is not needed anymore since git v2.0.1 (@see github.com/git/git/commit/… ) Commented Jun 18, 2020 at 14:05
Under OSX, to avoid this issue and avoid other problems with developing on a case-insensitive filesystem, you can use Disk Utility to create a case sensitive virtual drive / disk image.
Run disk utility, create new disk image, and use the following settings (or change as you like, but keep it case sensitive):
Make sure to tell git it is now on a case sensitive FS:
git config core.ignorecase false
-
15Nah, nuclear is running a fully case-sensitive boot drive on OSX. You'll have to live without poorly written (ahem, Adobe) apps, or run those in their own case-stupid VM, but it's worth it if you code primarily for *nix systems. Commented Aug 6, 2015 at 1:33
-
1This is the only option that properly works. I've tried the rest and you end up in a pickle one way or another. Solve the problem properly by doing this. Commented Jun 3, 2016 at 9:12
-
2Note that Disk Utility has a bug OS X 10.11 -- It won't create case sensitive images. You need to use the command line tool hdiutil. apple.stackexchange.com/questions/217915/…– dellsalaCommented Jan 18, 2017 at 20:51
-
7With APFS in High Sierra this is even easier. Click the icon of a drive with a plus and add a case-sensitive volume with no limits on size. It just shares space with the main volume and mounts at /Volumes/volume-name. Commented Aug 30, 2017 at 13:02
rename file
Name.jpg
toname1.jpg
commit removed file
Name.jpg
rename file
name1.jpg
toname.jpg
amend added file
name.jpg
to previous commitgit add name.jpg git commit --amend
-
2I am getting this
fatal: bad source, source=name1.jpg, destination=name.jpg
at step 3. Do you have suggestion? Thx Commented Mar 30, 2017 at 0:48 -
3
-
Worked for me. Honestly, its actually correct from an audit point of view. Commented Jan 14, 2021 at 18:16
-
1As CBarr's answer notes, you don't need the intermediate commit here. You can just rename, stage, rename, stage, then commit, without the extra commit in the middle that this answer uses. Commented May 3, 2021 at 16:04
Similar to @Sijmen's answer, this is what worked for me on OSX when renaming a directory (inspired by this answer from another post):
git mv CSS CSS2
git mv CSS2 css
Simply doing git mv CSS css
gave the invalid argument error: fatal: renaming '/static/CSS' failed: Invalid argument
perhaps because OSX's file system is case insensitive
p.s BTW if you are using Django, collectstatic also wouldn't recognize the case difference and you'd have to do the above, manually, in the static root directory as well
I tried the following solutions from the other answers and they didn't work:
If your repository is hosted remotely (GitHub, GitLab, BitBucket), you can rename the file on origin (GitHub.com) and force the file rename in a top-down manner.
The instructions below pertain to GitHub, however the general idea behind them should apply to any remote repository-hosting platform. Keep in mind the type of file you're attempting to rename matters, that is, whether it's a file type that GitHub deems as editable (code, text, etc) or uneditable (image, binary, etc) within the browser.
- Visit GitHub.com
- Navigate to your repository on GitHub.com and select the branch you're working in
- Using the site's file navigation tool, navigate to the file you intend to rename
- Does GitHub allow you to edit the file within the browser?
- a.) Editable
- Click the "Edit this file" icon (it looks like a pencil)
- Change the filename in the filename text input
- b.) Uneditable
- Open the "Download" button in a new tab and save the file to your computer
- Rename the downloaded file
- In the previous tab on GitHub.com, click the "Delete this file" icon (it looks like a trashcan)
- Ensure the "Commit directly to the
branchname
branch" radio button is selected and click the "Commit changes" button - Within the same directory on GitHub.com, click the "Upload files" button
- Upload the renamed file from your computer
- a.) Editable
- Ensure the "Commit directly to the
branchname
branch" radio button is selected and click the "Commit changes" button - Locally, checkout/fetch/pull the branch
- Done
-
-
Good to know. This technique should theoretically work on any repository hosting platform but I'd be interested to know if there are any that it would not work with. Commented Jan 2, 2018 at 19:11
-
Doesn't work for files that can't be edited in the browser, like images or PDF; there is no edit option, obviously. Commented Dec 26, 2018 at 20:16
-
@AbhijitSarkar Good point. I updated my answer for those cases. I tested and verified these instructions work. Commented Jan 11, 2019 at 20:36
With the following command:
git config --global core.ignorecase false
You can globally config your git system to be case sensitive for file and folder names.
Years later I need to come back to this question and provide yet another possible solution! I ran into this issue on a project where only a single file needed to be renamed. This is what I did and it worked for me.
git mv -f src/MyFile.js src/myfile.js
Which I learned both from this thread and this answer
Mac OSX High Sierra 10.13 fixes this somewhat. Just make a virtual APFS partition for your git projects, by default it has no size limit and takes no space.
- In Disk Utility, click the + button while the Container disk is selected
- Select APFS (Case-Sensitive) under format
- Name it
Sensitive
- Profit
- Optional: Make a folder in Sensitive called
git
andln -s /Volumes/Sensitive/git /Users/johndoe/git
Your drive will be in /Volumes/Sensitive/
How do I commit case-sensitive only filename changes in Git?
-
I love this suggestion, it elegantly and painlessly solves the problem without resort to ugly workarounds. Thanks! Commented Sep 24, 2018 at 23:42
-
1Just an advise, do not do that, Changing Hard format to
APFS (Case-sensitive)
makes your system very slow and you will face many weird issue, I faced ReactNative build issue, some addresses could not be found. so it's better to haveAPFS
the default of the company. for git case it's better to use this solution. Commented Jun 1, 2022 at 9:29 -
1@AmerllicA This doesn't change your system hard drive, only a partition to Case Sensitive. Performance is actually slightly better with case-senstivity on. The solution you linked to does not work for everyone as code that assumes linux-like case senstivity may not run, such as when two files are the same name but different case.– Ray FossCommented Jun 3, 2022 at 22:36
so there are many solutions to this case sensitivity deployment problem with how GitHub handles it.
In my case, I had changed the filename casing convention from uppercase to lowercase.
I do believe that git can track the change but this command
git config core.ignorecase false
dictates how git operates behind the scenes
In my case, I ran the command and git suddenly had lots of files to track labeled untracked.
I then hit git add. , then git committed and ran my build on netlify one more time.
Then all errors now displayed could be traced e.g
Module not found: Can't resolve './Components/ProductRightSide' in '/opt/build/repo/components/products
and fixed such that git was able to track and implement the changes successfully.
It's quite a workaround and a fingernail away from frustration but trust me this will surely work.
PS: after fixing your issue you may want to run the command
git config core.ignorecase true
to restore how git works with case sensitivity.
Also, note git config core.ignorecase false
has issues with other filename extensions so you may want to watch out, do it if you know what you’re doing and are sure of it.
Here's a thread on netlify that can help out, possibly
-
This is good but it seems to make the files as new, loosing all history rather than simply renaming the existing file with all of it's changes.– Craig.CCommented Jul 4, 2022 at 22:15
When you've done a lot of file renaming and some of it are just a change of casing, it's hard to remember which is which. manually "git moving" the file can be quite some work. So what I would do during my filename change tasks are:
- remove all non-git files and folder to a different folder/repository.
- commit current empty git folder (this will show as all files deleted.)
- add all the files back into the original git folder/repository.
- commit current non-empty git folder.
This will fix all the case issues without trying to figure out which files or folders you renamed.
-
1Why not
git commmit --amend
in In paragraph 4? Otherwise, there will be an extra commit with the removal of all files. Or you can usegit rebase -i
with squash. Commented Jun 3, 2017 at 17:06
I've faced this issue several times on MacOS. Git is case sensitive but Mac is only case preserving.
Someone commit a file: Foobar.java
and after a few days decides to rename it to FooBar.java
. When you pull the latest code it fails with The following untracked working tree files would be overwritten by checkout...
The only reliable way that I've seen that fixes this is:
git rm Foobar.java
- Commit it with a message that you cannot miss
git commit -m 'TEMP COMMIT!!'
- Pull
- This will pop up a conflict forcing you to merge the conflict - because your change deleted it, but the other change renamed (hence the problem) it
- Accept your change which is the 'deletion'
git rebase --continue
- Now drop your workaround
git rebase -i HEAD~2
anddrop
theTEMP COMMIT!!
- Confirm that the file is now called
FooBar.java
-
-1. I can't repro the error you mention here, and without it the rest of the answer doesn't make sense. Also, doing
git rebase --continue
without a rebase in progress just yields a "No rebase in progress?" error, and there's no rebase in progress during step 4.2, so running that command there doesn't make sense either. Commented May 3, 2021 at 15:59
Or simply rename the required file over git repository web UI interface and commit :)
I took @FiniteLooper answer and wrote a Python 3 Script to do it with a list of files:
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import os
import shlex
import subprocess
def run_command(absolute_path, command_name):
print( "Running", command_name, absolute_path )
command = shlex.split( command_name )
command_line_interface = subprocess.Popen(
command, stdout=subprocess.PIPE, cwd=absolute_path )
output = command_line_interface.communicate()[0]
print( output )
if command_line_interface.returncode != 0:
raise RuntimeError( "A process exited with the error '%s'..." % (
command_line_interface.returncode ) )
def main():
FILENAMES_MAPPING = \
[
(r"F:\\SublimeText\\Data", r"README.MD", r"README.md"),
(r"F:\\SublimeText\\Data\\Packages\\Alignment", r"readme.md", r"README.md"),
(r"F:\\SublimeText\\Data\\Packages\\AmxxEditor", r"README.MD", r"README.md"),
]
for absolute_path, oldname, newname in FILENAMES_MAPPING:
run_command( absolute_path, "git mv '%s' '%s1'" % ( oldname, newname ) )
run_command( absolute_path, "git add '%s1'" % ( newname ) )
run_command( absolute_path,
"git commit -m 'Normalized the \'%s\' with case-sensitive name'" % (
newname ) )
run_command( absolute_path, "git mv '%s1' '%s'" % ( newname, newname ) )
run_command( absolute_path, "git add '%s'" % ( newname ) )
run_command( absolute_path, "git commit --amend --no-edit" )
if __name__ == "__main__":
main()
If nothing worked use git rm filename to delete file from disk and add it back.
-
Try this answer if you ever come across this again stackoverflow.com/a/55541435/4484799 Commented Oct 3, 2021 at 22:03
I made a bash script to lowercase repository file names for me:
function git-lowercase-file {
tmp="tmp-$RANDOM-$1"
git mv -f $1 $tmp
git mv -f $tmp ${1,,}
}
then you can use it like this:
git-lowercase-file Name.jpg
If you have many files to rename, and a large repository, many of the answers here won't apply. I found a hack for my particular use case: where you already have a relatively easy way to correct the files on the filesystem (I happened to have a set of files with correct casing that I could drag over the top of the files tracked in the git repo, which fixed it for me on the f/s of macOS Catalina looking at an NTFS partition). Correcting the filesystem DID NOTHING to fix it in git's history, no matter what core.ignorecase
is set to, unless:
- Have a clean
git status
. Setgit config core.ignorecase false
- Do whatever you have to do to make the casing on your filesystem correct
- Note
git ls-files
shows you git is now tracking BOTH casings of files, despite only correct casing on f/s, this looks like a bad situation, but: git checkout .
: Git removes BOTH casings of all AFFECTED files from its history, Add and Commit- Do whatever you did in step 2 again to replace those files (perhpas a better command for the previous step might mitigate having to do this?) Your
git status
now shows as newly-added to git only those files with casing problems, instead of your whole filetree. Add and Commit again - Set
ignorecase
back to true if this f/s is where you work (recommended for case-insensitive filesystems (and we just saw a reason why!) and be more careful in future...
if you are like us and you are working on Azure Devops and you have a mixed team of apple and windows users, sometimes the filenames get the wrong casing, the easies way to solve it when you are in that environment is to rename it in the files page of the online Azure Devops portal, see on this screenshot
If you're doing more complex change, like directory name casing change, you can make that change from a Linux machine, because Linux itself (as well as git on Linux) treats files/directories with same names but different casing as completely different files/directories.
So if you're on Windows, you can install Ubuntu using WSL, clone your repo there, open the cloned repo directory using VSCode (use WSL remote extension to access WSL Ubuntu from Windows), then you will be able to make your renames through VSCode and commit/push them using VSCode git integration.
git mv
works.git
working copies. Spares a lot of my sanity, I must admit...