在 Git 中更改文件名的大小写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10523849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing capitalization of filenames in Git
提问by knpwrs
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 oras a new untracked file).
如您所见,Git 对此表示不满。我也尝试仅使用普通的旧mv
命令重命名,但 Git 不接受重命名(作为重命名或作为新的未跟踪文件)。
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.
如何更改文件以具有相同名称的不同大小写?我在 Mac OS X v10.7.3 (Lion) 和 Git 1.7.9.1 上使用Z shell(zsh) 4.3.15。
回答by VonC
Starting Git 2.0.1 (June 25th, 2014), a git mv
will just work on a case insensitive OS.
从 Git 2.0.1(2014 年 6 月 25 日)开始, agit mv
将只适用于不区分大小写的 OS。
See commit baa37bfby David Turner (dturner-tw
).
mv
: allow renaming to fix case on case insensitive filesystems
mv
: 允许重命名以修复不区分大小写的文件系统的大小写
"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.
“ git mv hello.txt Hello.txt
”在不区分大小写的文件系统始终触发“ destination already exists
”的错误,因为这两个名字指的是从看文件系统的角度相同的路径和需要用户给出“ --force
”纠正记录在索引和在路径的情况下,当下一次提交。
Detect this case and allow it without requiring "
--force
".
检测这种情况并允许它而不需要“
--force
”。
git mv hello.txt Hello.txt
just works (no --force
required anymore).
git mv hello.txt Hello.txt
只是工作(不再--force
需要)。
The other alternative is:
另一种选择是:
git config --global core.ignorecase false
And rename the file directly; git add and commit.
并直接重命名文件;git 添加并提交。
回答by Marcello de Sales
Considering larsks' answer, you can get it working with a single command with "--force":
考虑到larsks 的回答,您可以使用带有“--force”的单个命令使其工作:
git mv --force myfile MyFile
回答by MrHus
Sometimes you want to change the capitalization of a lot of file names 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:
有时您想在不区分大小写的文件系统(例如在 OS X 或 Windows 上)上更改许多文件名的大小写。执行git mv
命令会很快疲劳。为了让事情变得更容易,这就是我所做的:
- Move all files outside of the directory to, let's, 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.
- 将目录外的所有文件移动到桌面。
- 执行 a
git add . -A
删除所有文件。 - 将桌面上的所有文件重命名为正确的大小写。
- 将所有文件移回原始目录。
- 做一个
git add .
。Git 应该会看到文件被重命名。
Now you can make a commit saying you have changed the file name capitalization.
现在您可以进行提交,说明您已更改文件名的大小写。
回答by larsks
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.
OS X 下的文件名不区分大小写(默认情况下)。这与其说是 Git 问题,不如说是操作系统问题。如果您删除并读取文件,您应该得到您想要的内容,或者将其重命名为其他名称,然后将其重命名。
回答by Segmented
Set ignorecase
to false
in git config
在 git 配置中设置ignorecase
为false
As the original post is about "Changing capitalization of filenames in Git":
由于原始帖子是关于“在 Git 中更改文件名的大小写”:
If you are trying to change capitalisation of a filename in your project, you do not need to forcerename 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.
如果您尝试更改项目中文件名的大小写,则无需从 Git强制重命名它。IMO,我宁愿从我的 IDE/编辑器中更改大小写,并确保我正确配置 Git 以获取重命名。
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
:
默认情况下,Git 模板设置为忽略大小写(Git 不区分大小写)。要验证您是否拥有默认模板,请使用--get
检索指定键的值。使用--local
和--global
向 Git 指示是从本地 Git 存储库配置还是全局配置中获取配置键值。例如,如果要查找全局键core.ignorecase
:
git config --global --get core.ignorecase
If this returns true
, make sure to set it as:
如果返回true
,请确保将其设置为:
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.
(确保你有适当的权限来改变全局。)你有它;现在您的 Git 安装不会忽略大写并将它们视为更改。
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.
作为建议,如果您正在处理多语言项目,并且您觉得 Git 不应将所有项目都视为区分大小写,只需更新本地core.ignorecase
文件。
回答by Деян Добромиров
You can open the ".git" directory and then edit the "config" file. Under "[core]" set, set "ignorecase = true" and you are done ;)
您可以打开“.git”目录,然后编辑“config”文件。在“[core]”设置下,设置“ignorecase = true”就完成了;)
回答by softarn
To bulk git mv
files to lowercase on macOS:
git mv
在 macOS 上将文件批量转换为小写:
for f in *; do git mv "$f" "`echo $f | tr "[:upper:]" "[:lower:]"`"; done
It will lowercase all files in a folder.
它将小写文件夹中的所有文件。
回答by blented
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
.
这个 Python 代码片段将git mv --force
目录中的所有文件都变为小写。例如, foo/Bar.js 将通过 foo/bar.js 变成 foo/bar.js 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)
回答by Daniel Batista
Working example:
工作示例:
git mv ./src/images/poster_video.PNG ./src/images/poster_video.png