我认为提供有关如何重现问题的提示会很有帮助,以便更好地理解问题:
$ git init
$ echo "*.txt -text" > .gitattributes
$ echo -e "hello\r\nworld" > 1.txt
$ git add 1.txt
$ git commit -m "committed as binary"
$ echo "*.txt text" > .gitattributes
$ echo "change.." >> 1.txt
# Ok let's revert now
$ git checkout -- 1.txt
$ git status
modified: 1.txt
# Oooops, it didn't revert!!
# hm let's diff:
$ git diff
warning: CRLF will be replaced by LF in 1.txt.
The file will have its original line endings in your working
directory.
diff --git a/1.txt b/1.txt
index c78c505..94954ab 100644
--- a/1.txt
+++ b/1.txt
@@ -1,2 +1,2 @@
-hello
+hello
world
# No actual changes. Ahh, let's change the line endings...
$ file 1.txt
1.txt: ASCII text, with CRLF line terminators
$ dos2unix 1.txt
dos2unix: converting file 1.txt to Unix format ...
$ git diff
git diff 1.txt
diff --git a/1.txt b/1.txt
index c78c505..94954ab 100644
--- a/1.txt
+++ b/1.txt
@@ -1,2 +1,2 @@
-hello
+hello
world
# No, it didn't work, file is still considered modified.
# Let's try to revert for once more:
$ git checkout -- 1.txt
$ git status
modified: 1.txt
# Nothing. Let's use a magic command that prints wrongly committed files.
$ git grep -I --files-with-matches --perl-regexp '\r' HEAD
HEAD:1.txt
第二种重现方式:
在上面的脚本中替换这一行:
echo "*.txt -text" > .gitattributes
使用
git config core.autocrlf=false
并保持其余行不变
以上都说了什么?可以(在某些情况下)使用 CRLF 提交文本文件(例如-text
在.gitattributes
/ 或中core.autocrlf=false
)。
当我们稍后想要将相同的文件视为文本(-text
-> text
)时,将需要再次提交。
当然,您可以暂时恢复它(如Abu Assar正确回答)。在我们的例子中:
echo "*.txt -text" > .gitattributes
git checkout -- 1.txt
echo "*.txt text" > .gitattributes
答案是:你真的想这样做吗,因为每次更改文件都会导致同样的问题。
作为记录:
要检查哪些文件会在您的 repo 中导致此问题,请执行以下命令(git 应使用 --with-libpcre 编译):
git grep -I --files-with-matches --perl-regexp '\r' HEAD
通过提交文件(假设您想将它们视为文本),这与执行此链接http://help.github.com/line-endings/中提出的解决此类问题的方法相同. 但是,您可以只更改文件,然后执行然后提交,而不是删除.git/index
和执行。reset
git checkout -- xyz zyf