我认为提供提示如何重现该问题将很有帮助,以便更好地理解该问题:
$ 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
答案是:您是否真的要这样做,因为每次更改文件都会引起相同的问题。
作为记录:
要检查哪些文件会在您的仓库中导致此问题,请执行以下命令(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然后提交即可。
git diff --ignore-space-change或git diff --ignore-all-space使输出端的区别git diff?