由于大小写更改,git status 显示两个文件被修改

6
使用 Git 时,显然我在我的 Mac 上基于大小写敏感的文件名重命名文件时没有使用正确的方法。
以下是我目前遇到的情况的模拟,我不确定如何解决...
创建仓库并添加示例文件:
git init
touch Foo.js
git add .
git commit -m 'adding Foo.js'

我如何“重命名”文件(但应该使用git mv... :()

mv Foo.js foo.js
git status

Git状态没有显示任何更改,因此我更新了core.ignorecase属性

git config core.ignorecase false
git status

现在它显示我需要添加foo.js。
git add foo.js
git status
git commit -m 'renaming Foo.js to foo.js'

现在,如果我尝试编辑foo.js
vim foo.js <-- edit foo.js
git status

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   Foo.js
    modified:   foo.js

如果我尝试使用git rm Foo.js,它也会试图删除我的foo.js文件。

我当前的git --version2.1.2

你有什么想法可以让我解决这个问题,只删除Foo.js并保留foo.js吗?


在第二步中执行了 mv Foo.js foo.js 后,git status 没有显示 Foo.js 已被删除吗? - Arkadiusz Drabczyk
当我在执行mv命令后运行git status时,它会显示没有文件被删除。但是,当我设置git config core.ignorecase false时,只有文件foo.js需要添加(而Foo.js则不会出现在任何地方)。 - Jason Jarrett
你使用的是Windows还是Unix平台? - Antwane
在 Mac 上(这是一个不区分大小写的操作系统)- 这也在问题的第一行中指出 :) - Jason Jarrett
2个回答

4

在包含文件重命名的提交上使用“git revert”命令。 然后,您可以按照您在描述中指示的方式[正确地]重命名该文件并创建一个新的提交。

正确重命名文件的示例如下:

git mv --force Foo.js foo.js

3

Git 2.29(2020年第4季度)增加了另一种处理这种情况的方法:

请参见提交 087c616提交 409f066提交 5065ce4(由 brian m. carlson(bk2204 于2020年9月20日提交)。
(由 Junio C Hamano -- gitster -- 于2020年9月29日在 提交 c5a8f1e 中合并)

docs: 解释如何处理总是被修改的文件

签名:brian m. carlson

用户经常遇到仅大小写不同的两个文件,导致其中一个文件始终显示为已修改。因此,让我们添加一个 FAQ 条目来解释如何处理这种情况。

此外,让我们解释另一种通常导致文件总是修改的情况,即尚未通过清理或过滤器运行的文件。
同时解释如何修复此类问题。

现在,gitfaq 已经包含在其手册页中:

Why do I have a file that's always modified?

Internally, Git always stores file names as sequences of bytes and doesn't perform any encoding or case folding.

However, Windows and macOS by default both perform case folding on file names.
As a result, it's possible to end up with multiple files or directories whose names differ only in case.

Git can handle this just fine, but the file system can store only one of these files, so when Git reads the other file to see its contents, it looks modified.

It's best to remove one of the files such that you only have one file. You can do this with commands like the following (assuming two files AFile.txt and afile.txt) on an otherwise clean working tree:

$ git rm --cached AFile.txt
$ git commit -m 'Remove files conflicting in case'
$ git checkout .

This avoids touching the disk, but removes the additional file.
Your project may prefer to adopt a naming convention, such as all-lowercase names, to avoid this problem from occurring again; such a convention can be checked using a pre-receive hook or as part of a continuous integration (CI) system.

It is also possible for perpetually modified files to occur on any platform if a smudge or clean filter is in use on your system but a file was previously committed without running the smudge or clean filter.
To fix this, run the following on an otherwise clean working tree:

$ git add --renormalize .

1
谢谢,git rm --cached AFile.txt 是我要找的命令,可以摆脱旧文件名。 - ironic_ollins

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接