当 EOL 在 .gitattributes 中设置为 CRLF 时,Git diff 认为行尾是 LF

标签 git

当我恢复对具有 Windows 行结尾的文件的更改并且 .gitattributes 将 EOL 定义为 CRLF 时,git 认为行结尾已更改为 LR,即使十六进制编辑器显示 CRLF。

这仅在 .gitattributes 定义 EOL 字符时发生。

没有 .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

这不能正常工作。

将 CRLF 定义为 .gitattributes:

*.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>..  <

这是怎么回事,我该如何让它正常工作?

最佳答案

这里的关键是无论你使用text=autotext eol=crlf还是text eol=lf,git都会:

  1. 在存储库中将行结尾转换为 LF(即在 git commit 时)
  2. 从 存储库到你的工作树(即 git checkoutgit merge)

这可能是违反直觉的,但请记住 git 起源于 Linux 世界并且它不是错误。来自git documentation : "当一个文本文件被规范化时,它的行尾在存储库中被转换为 LF"。

作为对此的必然结果,我发现当我加入一个现有项目并且需要引入 .gitattributes 来规范化行尾时,我发现最好编写一个 Powershell 脚本(或任何你想要的方法) prefer) 一次标准化存储库中所有文件中的行结尾。这是为了避免持续的困惑差异,其中唯一的变化是 .gitattributes 引入的行尾。

最后,如果它有帮助,我之前在我的 GitHub 上发布了一个沙箱来使用行尾设置:https://github.com/teamtam/git-line-endings

关于当 EOL 在 .gitattributes 中设置为 CRLF 时,Git diff 认为行尾是 LF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40478605/

相关文章:

svn - git-svn 无法提交,即使在干净 checkout 后也是如此

git - 使用 git 管理多个项目,这些项目相互构建并且可以独立使用

git - 是否推荐将 Git 用于大型 (>250GB) 内容存储库

git - 每次提交时让 git 询问我的电子邮件

git - VS 代码 : explore commits in chronological order

linux - 错误打印消息

linux - 如何从源代码编译XBMC

git - Jenkins 中多分支管道的 merge 请求触发器

c++ - autoconf 项目的哪些文件要放入 .gitignore?

git - 为什么我所有的 git 存储库突然显示所有内容都已删除?