【问题标题】:Git files modified after checkout, reset --hard, etc. even though autocrlf is set to falseGit 文件在 checkout、reset --hard 等之后修改,即使 autocrlf 设置为 false
【发布时间】:2013-03-19 00:43:33
【问题描述】:

这是我的系统对话:

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 在结帐时修改文件?

【问题讨论】:

  • 可能是 this question 的副本,在其中一个得到对两者都适用的正确答案之前,不要投票结束。
  • 什么操作系统顺便说一句?我假设是 Windows?
  • @Ikke Mac/Unix。看来我家的 Mac 还是丢掉了额外的 CR,我可能把 nano 配置错了。
  • 同时检查core.safecrlf。理论上,由于core.eolcore.autocrlf 都关闭了,这应该不会有任何影响,但我还没有真正玩过这三个变量的所有可能组合...

标签: git newline core.autocrlf


【解决方案1】:

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

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

* text=auto 更改为#* text=auto 对文件行结尾进行必要的更改并推送您的提交。然后,您可以在进行更改后再次启用它,或者选择可能更适合您的项目的其他选项之一。

【讨论】:

  • 当然,如果您正在处理同一个 git 存储库,您会注意到实际上 是一个 .gitattributes 文件,只是我在搜索中没有找到。 .
  • 谢谢!我的情况略有不同:在我添加 .gitattributes 后,一个 .jar 文件突然显示为已修改。我在其中添加了一行: *.jar binary
【解决方案2】:

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

git diff path/to/file.html

如果您看到的唯一变化是旧模式/新模式,则可能是权限问题。您可以使用以下命令告诉 git 忽略文件权限更改:

git config core.filemode false

git config --global core.filemode false

(取决于你如何使用 git)。

出于性能原因,我最近从使用 Cygwin git 切换到 Windows 的 git,另外为了 TortoiseGit 可以正常工作,这可能是我的情况下权限被丢弃的原因。

参考资料:

  1. How do I remove files saying "old mode 100755 new mode 100644" from unstaged changes in Git?

【讨论】:

  • 感谢core.filemode,它解决了我在 Windows 上的问题。
猜你喜欢
  • 1970-01-01
  • 2012-06-15
  • 1970-01-01
  • 1970-01-01
  • 2011-07-04
  • 2018-07-02
  • 2011-08-19
  • 1970-01-01
  • 2018-11-24
相关资源
最近更新 更多