gitattributes 中的 `* text=auto eol=lf` 会做什么?

What will `* text=auto eol=lf` in gitattributes do?

我们的 .gitattributes 文件中有这个:

* text=auto eol=lf

我想准确了解它的作用。

第一部分是text=auto。来自 documentation:

This ensures that all files that Git considers to be text will have normalized (LF) line endings in the repository.

重要的部分是 Git 仅对它检测为文本文件的文件进行规范化。

但是,我不确定 eol=lf 部分。我认为它也只会对文本文件进行规范化,但我在文档中找不到对它的支持,而且我们有一个实例,当我们的 PNG 文件也被规范化时,使它们无效。

是否有像上面这样的设置基本上会说 "do the normalization in both directions for text files, and leave binary files alone"?

答案是否定的,Git 目前(从 2.3 开始)无法通过自动检测二进制和文本格式进行结帐 EOL 转换,因此它只能处理文本。解决方法是只为选定的文件类型(例如 *.txt)指定 eol=lf,或者相反,使用例如将某些文件类型标记为二进制文件。 *.png binary).

相关:feature proposal on Git mailing list


* text=auto

This will correctly normalize text files in the repo. However, the second part (LF forcing on checkout) cannot be achieved easily today because adding eol=lf will unfortunately process binary files too. The only solution today is to mark certain types for conversion (e.g., *.txt eol=lf) or, inversely, mark certain types as binary (e.g., *.png binary).

Both of these suffer from the same issue: the specific file types must be listed explicitly in the .gitattributes file, which means that either the types must be known ahead of time or all developers must remember to update the .gitattributes file every time a new file type appears in the project. Which they won't.

Git 2.10 fixed this 现在的行为符合人们的预期。

此答案适用于可能像我一样在这里跌跌撞撞的其他人。

从 Git 2.10 开始,这按预期工作,以下仅与 2.10 相关(使用 git --version 检查版本)。 Relevant snippet from Git 2.10 release notes

* text=auto eol=lf 表现为 git config core.autocrlf false

* text=auto eol=crlf 表现为 git config core.autocrlf true

This ensures that all files that Git considers to be text will have normalized (LF) line endings in the repository.

此引用中提到的 lf 指的是将文件添加到索引(并最终推送到上游)时发生的情况。附加的 eol=xx 说明这些文件应该在您的工作树中,即在检出 git 通过 * text=auto 部分自动检测为文本的文件后,它们应该如何在您的文件系统上本地显示.

Is there a settings like the above that would basically say "do the normalization in both directions for text files, and leave binary files alone"?

两者都可以,但我个人在 .gitattributes 文件中使用以下内容。顺序很重要。

* text=auto eol=lf
*.bat text eol=crlf
*.cmd text eol=crlf
*.ahk text eol=crlf