708

I am trying to rename a file to have different capitalization from what it had before:

git mv src/collision/b2AABB.js src/collision/B2AABB.js
fatal: destination exists, source=src/collision/b2AABB.js, destination=src/collision/B2AABB.js

As you can see, Git throws a fit over this. I tried renaming using just the plain old mv command as well, but Git doesn't pick up the rename (as a rename or as a new untracked file).

How can I change a file to have a different capitalization of the same name? I am on Mac OS X v10.7.3 (Lion) with Git 1.7.9.1 using Z shell (zsh) 4.3.15.

6

12 Answers 12

918

Starting Git 2.0.1 (June 25th, 2014), a git mv will just work on a case-insensitive OS.

See commit baa37bf by David Turner (dturner-tw).

mv: allow renaming to fix case on case-insensitive filesystems

"git mv hello.txt Hello.txt" on a case-insensitive filesystem always triggers "destination already exists" error, because these two names refer to the same path from the filesystem's point of view and requires the user to give "--force" when correcting the case of the path recorded in the index and in the next commit.

Detect this case and allow it without requiring "--force".

git mv hello.txt Hello.txt just works (no --force required anymore).


The other alternative is:

git config --global core.ignorecase false

And rename the file directly; git add and commit.

It does work in a CMD. It might fail in a git bash (on Windows) session (see Louis-Caron's answer)

As noted by jaquinocode in the comments, if your local repository itself has that setting:

git config --local core.ignorecase false
12
  • 7
    FWIW this regressed or never worked on Windows. Am on 2.15.1.windows.2 and still need to use --force
    – TTimo
    Dec 26, 2017 at 19:09
  • Will this work for the extensions capitalization also For example, if have file name image.TXT and I want to rename it like image.txt Jun 21, 2019 at 5:14
  • @siluverukirankumar Yes. Did you have any issue with that usecase?
    – VonC
    Jun 21, 2019 at 5:18
  • 12
    Got this to work on windows by running this command and git config core.ignorecase false May 5, 2020 at 11:13
  • 1
    @jaquinocode Good point. I have included your comment in the answer for more visibility.
    – VonC
    Dec 30, 2021 at 19:36
526

Considering larsks' answer, you can get it working with a single command with --force:

git mv --force myfile MyFile
3
  • 3
    If you are on a case insensitive file system and you get a fatal error "Invalid Argument" try these steps instead: stackoverflow.com/questions/3011625/…
    – Levi
    Dec 1, 2013 at 19:10
  • 4
    While this is the correct answer for the first step, how to you then proceed to switch to another branch under Mac OS X when the other branch has the old capitalization. I get **** error: The following untracked working tree files would be overwritten by checkout: ****
    – TJChambers
    Jan 17, 2014 at 2:06
  • 2
    This failed on commit for me on Windows: Error: Will not add file alias 'MyFile' ('myfile' already exists in index)
    – OrangeDog
    Feb 2, 2016 at 11:57
160

Sometimes you want to change the capitalization of a lot of file names on a case insensitive filesystem (e.g. on macOS or Windows). Doing individual git mv commands will tire quickly. To make things a bit easier this is what I do:

  1. Move all affected files outside of the directory to, let’s say, the desktop.
  2. Do a git add . -A to stage the removal of those files.
  3. Rename all files on the desktop to the proper capitalization.
  4. Move all the files back to the original directory.
  5. Do a git add .. Git should see that the files are renamed.

Now you can make a commit saying you have changed the file name capitalization.

5
  • 3
    This comes at the expense of all history in the files you move in and out, right? I'm guessing the git mv --force doesn't have this shortcoming.
    – LOAS
    Jan 4, 2018 at 9:21
  • 20
    No, this would not remove all of the history. Notice that there is no commit between the two adds. Jan 24, 2019 at 22:15
  • this is really what I needed. well done. it should be an answer as well. thank you! Jun 22, 2020 at 21:35
  • See also @softarn's answer, which offers a convenient one-liner to do this all at once :)
    – waldyrious
    Sep 27, 2021 at 10:00
  • 4
    The much simpler and less hacky way to do this is git rm -r --cached . + git add --all . as explained in this answer that deserves a ton more upvotes.
    – Inigo
    Jan 31 at 1:57
73

File names under OS X are not case sensitive (by default). This is more of an OS problem than a Git problem. If you remove and readd the file, you should get what you want, or rename it to something else and then rename it back.

4
  • 2
    You can also git clone the repo on a Linux system, rename the files and commit them just for this situation (if you have a Linux system at hand).
    – gitaarik
    Feb 7, 2014 at 12:48
  • 4
    Actually, filesystems on OS X can be case-sensitive, you can configure it on installation. How to check if an OS X partition is case-sensitive
    – Flimm
    May 5, 2015 at 17:19
  • 2
    ...hence the "(by default)" in the answer.
    – larsks
    Aug 18, 2017 at 16:45
  • 8
    To confirm, you can use git rm --cached fileorfolder to remove the file or folder from git without removing the file or folder from the file system. Then you can simply add the file or folder again with git add fileorfolder.
    – kas
    May 1, 2018 at 18:32
59

Set ignorecase to false in git config

As the original post is about "Changing capitalization of filenames in Git":

If you are trying to change capitalisation of a filename in your project, you do not need to force rename it from Git. IMO, I would rather change the capitalisation from my IDE/editor and make sure that I configure Git properly to pick up the renaming.

By default, a Git template is set to ignore case (Git case insensitive). To verify you have the default template, use --get to retrieve the value for a specified key. Use --local and --global to indicate to Git whether to pick up a configuration key-value from your local Git repository configuration or global one. As an example, if you want to lookup your global key core.ignorecase (if the commands return nothing, you probably need to prefix with sudo):

git config --global --get core.ignorecase

If this returns true, make sure to set it as:

git config --global core.ignorecase false

(Make sure you have proper permissions to change global.) And there you have it; now your Git installation would not ignore capitalisations and treat them as changes.

As a suggestion, if you are working on multi-language projects and you feel not all projects should be treated as case-sensitive by Git, just update the local core.ignorecase file.

6
  • 11
    And if git config --global --get core.ignorecase doesn't return anything. Is it true?? because after I set it to false it returns false (Windows 10)
    – fralbo
    Jan 7, 2017 at 10:47
  • 2
    And it seems not working as expected. I mean the desktop client see the change but after commit/sync, the filename remains unchanged online !
    – fralbo
    Jan 7, 2017 at 12:33
  • Worked for me (and better for multiple files than using git mv). I'm also assuming that by default it is considered true (i.e. it is ignoring case). Either that, or it matches the OS's policy to file name casing.
    – Jerry
    Dec 2, 2019 at 10:10
  • 1
    This should be the selected answer Apr 17, 2020 at 16:00
  • 2
    Changing the global setting may not work because the local setting is overriding it. If it is not working for you check both.
    – Xcalibur
    Nov 6, 2020 at 3:04
12

You can open the ".git" directory and then edit the "config" file. Under "[core]" set, set "ignorecase = true" and you are done ;)

2
  • 5
    seems that it should be changed to false such that git would be case-sensitive?
    – Jonathan
    Jun 9, 2020 at 8:02
  • 1
    Yep, exactly. I personally have when git invokes a smart behavior and attempt to convert line endings of my files. A shitty smart feature that I currently detest very much, as I mainly code under Windows for Linux machines so I say checkout as is, commit as is. Apr 3, 2021 at 11:51
8

Answer by Vonc is totally correct, but there is still a potential situation where your rename action would not work with git for windows:

Let's say you want to rename dir/mypath to dir/myPath:

git mv dir/mypath dir/myPath

but it fails reporting:

Rename from 'dir/mypath' to 'dir/mypath' failed. Should I try again? (y/n) 

The problem is that the bash has silently replaced your command line dir/myPath with dir/mypath because it has detected that such a path exists with a different capitalization.

The solution is to use an intermediate move operation:

git mv dir/mypath dir/mypath_temp
git mv dir/mypath_temp dir/myPath
0
7

To bulk git mv files to lowercase on macOS and git bash on Windows:

for f in *; do git mv "$f" "`echo $f | tr "[:upper:]" "[:lower:]"`"; done

It will lowercase all files in a folder.

2
  • 1
    it also works on windows in git bash. thanks for saving me a lot of time! :) Mar 16, 2021 at 9:44
  • @VincentGagnon glad to hear! Updated the answer with that info.
    – softarn
    Mar 16, 2021 at 9:57
2

Working example:

git mv ./src/images/poster_video.PNG ./src/images/poster_video.png
2
  • 2
    Your answer does not solve the original question. You are lowcasing the extension
    – Tavo
    Nov 26, 2019 at 10:38
  • 1
    I am looking exactly this, but for 500 images. i want to rename ./src/images/TESTIMAGE.png to ./src/images/testimage.png on git Dec 28, 2019 at 13:43
2

If you want to change the file name to uppercase , you can simply go to github repo & press . i.e dot =>this will open up the online editor. Make your changes & commit your changes there only using source control icon on the left.

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Mar 1 at 13:09
1

This Python snippet will git mv --force all files in a directory to be lowercase. For example, foo/Bar.js will become foo/bar.js via git mv foo/Bar.js foo/bar.js --force.

Modify it to your liking. I just figured I'd share :)

import os
import re

searchDir = 'c:/someRepo'
exclude = ['.git', 'node_modules','bin']
os.chdir(searchDir)

for root, dirs, files in os.walk(searchDir):
    dirs[:] = [d for d in dirs if d not in exclude]
    for f in files:
        if re.match(r'[A-Z]', f):
            fullPath = os.path.join(root, f)
            fullPathLower = os.path.join(root, f[0].lower() + f[1:])
            command = 'git mv --force ' + fullPath + ' ' + fullPathLower
            print(command)
            os.system(command)
1

I got the following changes on Windows in Sourcetree:

enter image description here

I solved it by removing the file from the file system:

enter image description here

Then simply discard the file I want to keep and commit:

enter image description here

Now everything worked as expected.

enter image description here

Based on this answer:

https://stackoverflow.com/a/66121726/3850405

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.