问题
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 10.7.3 with git 1.7.9.1 using zsh 4.3.15.
回答1:
Starting git 2.0.1 (June 25th, 2014), a git mv
will just work on 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).
回答2:
Considering the answers above, You can get it working with a single command with "--force":
git mv --force myfile MyFile
回答3:
Sometimes you want to change the capitalization of a lot of files on a case insensitive filesystem (e.g. on OS X or Windows). Doing git mv
commands will tire quickly, to make things a bit easier this is what I do:
- Move all files outside of the directory to lets say the Desktop.
- Do a
git add . -A
to remove all files. - Rename all files on the Desktop to the proper capitalization.
- Move all the files back to the original directory.
- Do a
git add .
git should see that the files are renamed.
Now you can make a commit saying you have changed the file capitalization.
回答4:
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 re-add the file you should get what you want, or rename it to something else and thn rename it back.
回答5:
As the OP 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 git whether to pick up config key-value from your local git repo config or global one. As, an example if you want to lookup your global key core.ignorecase
:
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 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
回答6:
You can open the ".git" directory and then edit the "config" file. Under "[core]" set "ignorecase = true" and you are done ;)
回答7:
This python snippet will git mv --force
all files in a directory to be lowercase, ex: foo/Bar.js will become foo/bar.js via git mv foo/Bar.js foo/bar.js --force
Modify it to your liking, 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)
回答8:
To bulk git mv
files to lowercase on macOS:
for f in *; do git mv "$f" "`echo $f | tr "[:upper:]" "[:lower:]"`"; done
It will lowercase all files in a folder.
回答9:
working example:
git mv ./src/images/poster_video.PNG ./src/images/poster_video.png
来源:https://stackoverflow.com/questions/10523849/changing-capitalization-of-filenames-in-git