即使设置了 autocrlf 为 false,使用 checkout、reset --hard 等命令后 Git 文件仍然被修改了。

18

以下是我的系统对话:

unrollme-dev-dan:views Dan$ git reset --hard HEAD
HEAD is now at 3f225e9 Fix scan titles
unrollme-dev-dan:views Dan$ git status
# On branch master
# 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:   app/signup/finish.html
#   modified:   app/signup/scan.html
#

我将autocrlf设置为false:

unrollme-dev-dan:unroll-website Dan$ git config core.autocrlf
unrollme-dev-dan:unroll-website Dan$ 
unrollme-dev-dan:unroll-website Dan$ git config --global core.autocrlf
unrollme-dev-dan:unroll-website Dan$

而且我没有任何.gitattributes文件搞砸这个:

unrollme-dev-dan:unroll-website Dan$ find . -name .gitattributes
[ only results are in different directory tree ]

下面的回答指出,这是由一个位于上一级的.gitattributes引起的。

当我对文件运行 od -c 命令时,它显示为 \r\n。我不知道它们应该是什么,可能是应该以 \n 结尾,这就是为什么会出现差异。但我不明白的是,即使设置了autocrlf为false,这些文件也如何可能在检出时被修改。

除了autocrlf之外,还有什么原因会导致Git在检出时修改文件?


可能是这个问题的重复,不投票关闭直到其中一个有适用于两者的正确答案。 - djechlin
操作系统是什么?我猜是Windows吧? - Ikke
@不是Mac/Unix。看起来我的家用Mac多了一些回车符,我可能配置了nano错误。 - djechlin
还要检查 core.safecrlf。理论上,由于 core.eolcore.autocrlf 都关闭了,这应该没有任何影响,但我还没有真正尝试过三个变量的所有可能组合... - twalberg
2个回答

21

这个问题可能是由于gitattributes的text选项引起的。https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html

可以通过临时编辑项目文件夹中的.gitattributes文件来解决这个问题。

* text=auto更改为#* text=auto并对文件行尾进行必要的更改,然后提交您的更改。在更改完成后,可以重新启用它,或者选择其他更适合您的项目的选项。


当然,如果您正在处理相同的 Git 存储库,您可能会注意到实际上确实存在 .gitattributes 文件,只是我在搜索中没有找到它... - djechlin
谢谢!我的情况有些不同:我添加了.gitattributes文件后,一个.jar文件突然显示为已修改。我在.gitattributes中添加了以下行: *.jar binary - savedario

4

我没有像被接受的答案中提到的那样有一个gitattributes文件,这是我的文件权限问题。要查看是否存在此问题,请使用git diff检查更改文件的差异,例如:

git diff path/to/file.html

如果您只看到旧模式/新模式的更改,则很可能是权限问题。您可以告诉git忽略文件权限更改,使用以下命令:
git config core.filemode false

或者

git config --global core.filemode false

(这取决于您如何使用git)。

出于性能原因,以及为了让TortoiseGit正常工作,我最近从Cygwin git切换到了git for Windows,这可能是权限在我的情况下被破坏的原因。

参考资料:

  1. 如何在Git中从未提交的更改中删除文件,显示“旧模式100755新模式100644”?

感谢core.filemode,它解决了我在Windows上的问题。 - lud

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