这可能是由以下原因引起的.gitattributes
文件向 git 指示它应该进行 EOL 标准化,但存储库包含非标准化行结尾。
简单的修复方法是删除相关行.gitattributes
。这可能是
* text=auto
or
*.cs text
下面是如何发生这种情况的一个简单示例:
$ echo "Hello World" > example.txt
$ unix2dos example.txt #Make sure it uses CRLF
$ git add example.txt
$ git commit -m "commit 1"
$ #Instruct git that all .txt files should be normalized
$ echo '*.txt text' >> .gitattributes
$ git add .gitattributes
$ git commit -m "commit 2"
现在存储库处于一种奇怪的状态,因为.gitattributes
声称在将文件添加到索引之前应该对其进行规范化,但当前提交的版本未规范化。
然而,此时,git status
没有注意到这一点,因为文件本身的大小或 mtime 自从添加到索引以来没有改变,所以索引被认为是最新的:
$ git status
On branch master
nothing to commit, working directory clean
但是任何使索引无效的行为都会导致 git 认为该文件是脏的:
$ touch example.txt
On branch master
Changes not staged for commit:
modified: example.txt
no changes added to commit (use "git add" and/or "git commit -a")
And git reset --hard
或尝试将文件重置为其应有状态的任何其他操作都无法解决此问题。这是因为无法将文件添加到存储库中当前状态的索引中,因为 git 已被指示规范化该文件,并且规范化永远无法生成当前提交的对象。
这就是为什么GITATTRIBUTES(1)
手册页建议在引入行结束规范化时显式使整个索引无效,如下所示:
$ echo "* text=auto" >>.gitattributes
$ rm .git/index # Remove the index to force Git to
$ git reset # re-scan the working directory
$ git status # Show files that will be normalized
$ git add -u
$ git add .gitattributes
$ git commit -m "Introduce end-of-line normalization"
阅读“行尾转换”部分git 属性 http://git-scm.com/docs/gitattributes手册页了解更多详细信息。
而不是采用仅删除该行的快速解决方案.gitattributes
,您可能想保留行结束标准化规则并立即对其进行标准化。这基本上只是意味着提交 20 多个不会消失的更改,但您可以按照上述有关引入行结束规范化的说明(减去编辑.gitattributes
),然后确信这种情况不会再次发生,因为所有文件现在都以标准化结尾提交,并且您将来添加的任何文件也将被标准化。这主要是个人喜好。