如何在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 16:34:38  来源:igfitidea点击:

How do I commit case-sensitive only filename changes in Git?

gitcase-sensitivegit-commitgit-config

提问by Gil Shulman

I have changed a few files name by de-capitalize the first letter, as in Name.jpgto 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 Keith Smiley

You can use git mv:

您可以使用git mv

git mv -f OldFileNameCase newfilenamecase

回答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 configdocumentation:

git config文档

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 makefilewhen git expects Makefile, git will assume it is really the same file, and continue to remember it as Makefile.

The default is false, except git-clone(1)or git-init(1)will probe and set core.ignorecasetrue 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 我能够从用户界面完成这一切

  1. RenameFILE.extto whatever.ext
  2. Stagethat file
  3. Now renamewhatever.extto file.ext
  4. Stagethat file again
  1. 重命名FILE.extwhatever.ext
  2. 暂存该文件
  3. 现在重命名whatever.extfile.ext
  4. 再次暂存该文件

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 --cachedor 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 mybranchto turn off case-sensitivity for a single checkoutcommand. 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):

要更长时间地更改设置(例如,如果在更改回之前需要运行多个命令):

  1. git config core.ignorecase(this returns the current setting, e.g. false).
  2. git config core.ignorecase<<true or false>>- set the desired new setting.
  3. ...Run multiple other commands...
  4. git config core.ignorecase<<false or true>>- set config value back to its previous setting.
  1. git config core.ignorecase(这将返回当前设置,例如false)。
  2. git config core.ignorecase<<true or false>>- 设置所需的新设置。
  3. ...运行多个其他命令...
  4. 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):

运行磁盘实用程序,创建新的磁盘映像,并使用以下设置(或根据需要进行更改,但要区分大小写):

Mac Disk Utility Screenshot

Mac 磁盘工具屏幕截图

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 认为在浏览器中可编辑(代码、文本等)还是不可编辑(图像、二进制等)的文件类型。

  1. Visit GitHub.com
  2. Navigate to your repository on GitHub.com and select the branch you're working in
  3. Using the site's file navigation tool, navigate to the file you intend to rename
  4. Does GitHub allow you to edit the file within the browser?
    • a.) Editable
      1. Click the "Edit this file" icon (it looks like a pencil)
      2. Change the filename in the filename text input
    • b.) Uneditable
      1. Open the "Download" button in a new tab and save the file to your computer
      2. Rename the downloaded file
      3. In the previous tab on GitHub.com, click the "Delete this file" icon (it looks like a trashcan)
      4. Ensure the "Commit directly to the branchnamebranch" radio button is selected and click the "Commit changes" button
      5. Within the same directory on GitHub.com, click the "Upload files" button
      6. Upload the renamed file from your computer
  5. Ensure the "Commit directly to the branchnamebranch" radio button is selected and click the "Commit changes" button
  6. Locally, checkout/fetch/pull the branch
  7. Done
  1. 访问 GitHub.com
  2. 导航到您在 GitHub.com 上的存储库并选择您正在工作的分支
  3. 使用站点的文件导航工具,导航到您要重命名的文件
  4. GitHub 是否允许您在浏览器中编辑文件?
    • a.) 可编辑
      1. 单击“编辑此文件”图标(它看起来像一支铅笔)
      2. 更改文件名文本输入中的文件名
    • b.) 不可编辑
      1. 在新选项卡中打开“下载”按钮并将文件保存到您的计算机
      2. 重命名下载的文件
      3. 在 GitHub.com 上的上一个选项卡中,单击“删除此文件”图标(它看起来像一个垃圾桶)
      4. 确保branchname选中“直接提交到分支”单选按钮,然后单击“提交更改”按钮
      5. 在 GitHub.com 的同一目录中,单击“上传文件”按钮
      6. 从您的计算机上传重命名的文件
  5. 确保branchname选中“直接提交到分支”单选按钮,然后单击“提交更改”按钮
  6. 在本地,checkout/fetch/pull 分支
  7. 完毕

回答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 cssgave the invalid argument error: fatal: renaming '/static/CSS' failed: Invalid argumentperhaps 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.jpgto name1.jpg

1)将文件重命名Name.jpgname1.jpg

2) commit removed file Name.jpg

2)提交删除的文件 Name.jpg

3) rename file name1.jpgto name.jpg

3)将文件重命名name1.jpgname.jpg

4) ammend added file name.jpgto previous commit

4)将添加的文件修改name.jpg为以前的提交

git add
git commit --amend