当我还原一个以Windows换行符结尾的文件时,如果.gitattributes将EOL定义为CRLF,则git会认为换行符已更改为LR,即使十六进制编辑器显示为CRLF也是如此。只有在.gitattributes定义EOL字符时才会发生这种情况。
没有.gitattributes:
这个工作正常。
这是我的文件Web.config的原始版本。最后两个字符是0d 0a(CR LF):
但是在十六进制编辑器中情况并非如此:
这里发生了什么事情,我该如何使它正常工作?
没有.gitattributes:
这个工作正常。
这是我的文件Web.config的原始版本。最后两个字符是0d 0a(CR LF):
00000000: efbb bf3c 3f78 6d6c 2076 6572 7369 6f6e ...<?xml version
00000010: 3d22 312e 3022 2065 6e63 6f64 696e 673d ="1.0" encoding=
00000020: 2275 7466 2d38 223f 3e0d 0a "utf-8"?>..
我在第一行的末尾添加了一个空格字符,20 0d 0a
:
00000000: efbb bf3c 3f78 6d6c 2076 6572 7369 6f6e ...<?xml version
00000010: 3d22 312e 3022 2065 6e63 6f64 696e 673d ="1.0" encoding=
00000020: 2275 7466 2d38 223f 3e20 0d0a "utf-8"?> ..
Git diff 显示空格字符:
diff --git a/Web.config b/Web.config
index bc3c3c3..6215f5e 100644
--- a/Web.config
+++ b/Web.config
@@ -1,4 +1,4 @@
<U+FEFF><?xml version="1.0" encoding="utf-8"?>{+ +}
撤销文件并且所有更改都将消失:
$ git checkout Web.config
$ git status Web.config
On branch develop
Your branch is up-to-date with 'origin/develop'.
nothing to commit, working directory clean
使用 .gitattributes
这种方法不起作用。
在 .gitattributes 中定义 CRLF:
*.config eol=crlf
在第一行末尾添加空格字符:
00000000: efbb bf3c 3f78 6d6c 2076 6572 7369 6f6e ...<?xml version
00000010: 3d22 312e 3022 2065 6e63 6f64 696e 673d ="1.0" encoding=
00000020: 2275 7466 2d38 223f 3e20 0d0a "utf-8"?> ..
Git diff 显示出了空格,但是CR(回车符)却没有(^M):
diff --git a/Web.config b/Web.config
index bc3c3c3..9d3bc53 100644
--- a/Web.config
+++ b/Web.config
@@ -1,248 +1,248 @@
<U+FEFF><?xml version="1.0" encoding="utf-8"?>[-^M-]{+ +}
撤销文件:
$ git checkout Web.config
$ git status Web.config
On branch develop
Your branch is up-to-date with 'origin/develop'.
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: Web.config
no changes added to commit (use "git add" and/or "git commit -a")
Git认为CR已从所有行中删除:
$ git diff --word-diff-regex=. Web.config
diff --git a/Web.config b/Web.config
index bc3c3c3..094d1d5 100644
--- a/Web.config
+++ b/Web.config
@@ -1,248 +1,248 @@
<U+FEFF><?xml version="1.0" encoding="utf-8"?>[-^M-]
<!--[-^M-]
For more information on how to configure your ASP.NET application, please visit[-^M-]
http://go.microsoft.com/fwlink/?LinkId=152368[-^M-]
-->[-^M-]
<configuration>[-^M-]
但是在十六进制编辑器中情况并非如此:
00000000: efbb bf3c 3f78 6d6c 2076 6572 7369 6f6e ...<?xml version
00000010: 3d22 312e 3022 2065 6e63 6f64 696e 673d ="1.0" encoding=
00000020: 2275 7466 2d38 223f 3e0d 0a3c 212d 2d0d "utf-8"?>..<!--.
00000030: 0a20 2046 6f72 206d 6f72 6520 696e 666f . For more info
00000040: 726d 6174 696f 6e20 6f6e 2068 6f77 2074 rmation on how t
00000050: 6f20 636f 6e66 6967 7572 6520 796f 7572 o configure your
00000060: 2041 5350 2e4e 4554 2061 7070 6c69 6361 ASP.NET applica
00000070: 7469 6f6e 2c20 706c 6561 7365 2076 6973 tion, please vis
00000080: 6974 0d0a 2020 6874 7470 3a2f 2f67 6f2e it.. http://go.
00000090: 6d69 6372 6f73 6f66 742e 636f 6d2f 6677 microsoft.com/fw
000000a0: 6c69 6e6b 2f3f 4c69 6e6b 4964 3d31 3532 link/?LinkId=152
000000b0: 3336 380d 0a20 202d 2d3e 0d0a 3c63 6f6e 368.. -->..<con
000000c0: 6669 6775 7261 7469 6f6e 3e0d 0a20 203c figuration>.. <
这里发生了什么事情,我该如何使它正常工作?
.gitattributes
中创建了一些CRLF文件text
,则应将其转换为blob中的LF。 - George Sovetov