Git autocrlf=true 按原样检出具有混合行结尾的文件

Git with autocrlf=true checks out files with mixed line endings as-is

所以,我一直认为 core.autocrlf=true Git 在将文件检出到工作目录时用 CRLF 替换所有 LF 结尾。

来自Git book:

If you’re on a Windows machine, set it to true – this converts LF endings into CRLF when you check out code

但是,当检出一个带有混合行结尾且 core.autocrlf 设置为 true 的文件时,我的 Git 版本按原样检出文件。

我找到了一个非常方便的 GitHub 存储库来测试此行为 - https://github.com/YueLinHo/TestAutoCrlf

测试结果:

到目前为止一切顺利,一切如我所料。现在对于具有混合行结尾的文件:

为什么会这样?我还没有看到任何有关 autocrlf=true 不使用混合行结尾的文件的信息。

我的 Git 设置有问题吗?在全局 .gitconfig 中使用 autocrlf=true 检查后,我检查了存储库文件夹中的 core.autocrlf 设置 运行 git config --get core.autocrlf,命令返回 true。没有 .gitattributes 文件来覆盖设置。

所有测试均在 Git 版本 1.9.5.msysgit.0 上进行。

编辑: 在最新的 msysgit 版本上有相同的行为 1.9.5.msysgit.1

我最初的问题是我以某种方式设法提交了一个只有 LF 结尾的混合行结尾文件,同时将 core.autocrlf 设置为 true,这意味着该文件已被签出按原样,但已将 CRLF 更改为 LF。我目前正在另一台机器上工作,无法在我的 msysgit 版本上重现此行为。

core.safecrlf 的值是多少?

如果core.safecrlf设置为true,则混合行结尾文件将不会被转换。 (因为如果行尾混合,转换是不可逆的)

我重新发布一个被其所有者删除的答案,因为我认为它给出了最好的解释。不知道作者为什么删了,我觉得是对的,我决定不删

显然,此行为已在 Git 中进行了硬编码,并且不依赖于 core.safecrlf(并且我对此进行了测试,即使我设置了 git config core.safecrlf false,混合文件也不会受到影响。

原回答如下:


Autocrlf 不会 转换混合行结尾,正如 git 的源代码所说:

https://github.com/git/git/commit/a0ad53c18100226cb1a138cb9b3bc3615170be8f

注意这里的评论:

/* No "naked" LF? Nothing to convert, regardless. */

/* If we have any CR or CRLF line endings, we do not touch it */
/* This is the new safer autocrlf-handling */

混合行结尾转换是 non-reversible,完成后,Git 崩溃了。

因此,如果您想自动转换文件的行尾,设置一个处理 end-of-line 的 .gitattributes 文件可能是个好主意。 如:

LF.txt eol=lf
CRLF.txt eol=crlf