在 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

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

Changing capitalization of filenames in Git

gitrenamecase-sensitivefile-rename

提问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 mvcommand 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 mvwill just work on a case insensitive OS.

从 Git 2.0.1(2014 年 6 月 25 日)开始, agit mv将只适用于不区分大小写的 OS

See commit baa37bfby David Turner (dturner-tw).

提交baa37bf大卫·特纳(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.txtjust works (no --forcerequired 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 mvcommands will tire quickly. To make things a bit easier this is what I do:

有时您想在不区分大小写的文件系统(例如在 OS X 或 Windows 上)上更改许多文件名的大小写。执行git mv命令会很快疲劳。为了让事情变得更容易,这就是我所做的:

  1. Move all files outside of the directory to, let's, say the desktop.
  2. Do a git add . -Ato remove all 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.
  1. 将目录外的所有文件移动到桌面。
  2. 执行 agit add . -A删除所有文件。
  3. 将桌面上的所有文件重命名为正确的大小写。
  4. 将所有文件移回原始目录。
  5. 做一个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 ignorecaseto falsein git config

在 git 配置中设置ignorecasefalse

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 --getto retrieve the value for a specified key. Use --localand --globalto 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.ignorecasefile.

作为建议,如果您正在处理多语言项目,并且您觉得 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 mvfiles 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 --forceall 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