如何在 Git 中提交仅区分大小写的文件名更改?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17683458/
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
How do I commit case-sensitive only filename changes in Git?
提问by Gil Shulman
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.
我通过取消大写第一个字母Name.jpg
来更改一些文件名,如name.jpg
. Git 无法识别此更改,我不得不删除文件并再次上传它们。在检查文件名更改时,有没有办法让 Git 区分大小写?我没有对文件本身进行任何更改。
回答by Gil Shulman
Git has a configuration setting that tells it whether to be case sensitive or insensitive: core.ignorecase
. To tell Git to be case-senstive, simply set this setting to false
:
Git 有一个配置设置,告诉它是区分大小写还是不区分大小写:core.ignorecase
. 要告诉 Git 区分大小写,只需将此设置设置为false
:
git config core.ignorecase false
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.
core.ignorecase
如果为 true,则此选项启用各种变通方法,以使 git 在不区分大小写的文件系统(如 FAT)上更好地工作。例如,如果一个目录列表
makefile
在 git 期望的时候找到Makefile
,git 会假设它确实是同一个文件,并继续将它记住为Makefile
.默认为 false,除了git-clone(1)或git-init(1)将
core.ignorecase
在创建存储库时探测并设置为true。
Case-insensitive file-systems
不区分大小写的文件系统
The two most popular operating systems that have case-insensitive file systems that I know of are
我所知道的两个最流行的具有不区分大小写文件系统的操作系统是
- Windows
- OS X
- 视窗
- 操作系统
回答by CBarr
Using SourceTree I was able to do this all from the UI
使用 SourceTree 我能够从用户界面完成这一切
- Rename
FILE.ext
towhatever.ext
- Stagethat file
- Now rename
whatever.ext
tofile.ext
- Stagethat file again
- 重命名
FILE.ext
为whatever.ext
- 暂存该文件
- 现在重命名
whatever.ext
为file.ext
- 再次暂存该文件
It's a bit tedious, but if you only need to do it to a few files it's pretty quick
这有点乏味,但是如果您只需要对几个文件进行操作,那就很快了
回答by Sijmen Mulder
This is what I did on OS X:
这是我在 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.
两个步骤,否则我会收到“文件存在”错误。也许可以通过添加--cached
等一步完成。
回答by Steve Chambers
It is sometimes useful to temporarilychange Git's case sensitivity:
临时更改 Git 的区分大小写有时很有用:
Method #1 - Change case sensitivity for a single command:
方法 #1 - 更改单个命令的区分大小写:
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.)
git -c core.ignorecase=true checkout mybranch
关闭单个checkout
命令的区分大小写。或者更一般地说:。(感谢 VonC 在评论中提出这一点。)git -c core.ignorecase=
<<true or false>>
<<command>>
Method #2 - Change case sensitivity for multiple commands:
方法#2 - 更改多个命令的区分大小写:
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.
git config core.ignorecase
(这将返回当前设置,例如false
)。git config core.ignorecase
<<true or false>>
- 设置所需的新设置。- ...运行多个其他命令...
git config core.ignorecase
<<false or true>>
- 将配置值设置回之前的设置。
回答by user1821510
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 sensitivevirtual drive/ disk image.
在 OSX 下,为了避免这个问题并避免在不区分大小写的文件系统上开发的其他问题,您可以使用磁盘工具来创建区分大小写的虚拟驱动器/磁盘映像。
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 它现在在区分大小写的 FS 上:
git config core.ignorecase false
回答by gmeben
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.
如果您的存储库是远程托管的(GitHub、GitLab、BitBucket),您可以重命名原始文件 (GitHub.com) 并以自上而下的方式强制重命名文件。
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.
下面的说明适用于 GitHub,但它们背后的总体思路应该适用于任何远程存储库托管平台。请记住,您尝试重命名的文件类型很重要,也就是说,它是 GitHub 认为在浏览器中可编辑(代码、文本等)还是不可编辑(图像、二进制等)的文件类型。
- 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
- 访问 GitHub.com
- 导航到您在 GitHub.com 上的存储库并选择您正在工作的分支
- 使用站点的文件导航工具,导航到您要重命名的文件
- GitHub 是否允许您在浏览器中编辑文件?
- a.) 可编辑
- 单击“编辑此文件”图标(它看起来像一支铅笔)
- 更改文件名文本输入中的文件名
- b.) 不可编辑
- 在新选项卡中打开“下载”按钮并将文件保存到您的计算机
- 重命名下载的文件
- 在 GitHub.com 上的上一个选项卡中,单击“删除此文件”图标(它看起来像一个垃圾桶)
- 确保
branchname
选中“直接提交到分支”单选按钮,然后单击“提交更改”按钮 - 在 GitHub.com 的同一目录中,单击“上传文件”按钮
- 从您的计算机上传重命名的文件
- a.) 可编辑
- 确保
branchname
选中“直接提交到分支”单选按钮,然后单击“提交更改”按钮 - 在本地,checkout/fetch/pull 分支
- 完毕
回答by Anupam
Similar to @Sijmen's answer, this is what worked for me on OSX when renaming a directory(inspired by thisanswer from another post):
与@Sijmen 的回答类似,这就是重命名目录时在 OSX 上对我有用的方法(灵感来自另一篇文章的这个答案):
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
简单地做git mv CSS css
给出了无效的参数错误:fatal: renaming '/static/CSS' failed: Invalid argument
也许是因为 OSX 的文件系统不区分大小写
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
ps 顺便说一句,如果您使用的是 Django,collectstatic 也无法识别大小写差异,您还必须在静态根目录中手动执行上述操作
回答by andrewvergel
I used those following steps:
我使用了以下步骤:
git rm -r --cached .
git add --all .
git commit -a -m "Versioning untracked files"
git push origin master
For me is a simple solution
对我来说是一个简单的解决方案
回答by razon
1) rename file Name.jpg
to name1.jpg
1)将文件重命名Name.jpg
为name1.jpg
2) commit removed file Name.jpg
2)提交删除的文件 Name.jpg
3) rename file name1.jpg
to name.jpg
3)将文件重命名name1.jpg
为name.jpg
4) ammend added file name.jpg
to previous commit
4)将添加的文件修改name.jpg
为以前的提交
git add
git commit --amend