Git * gitattributes 文件中的 text=auto 和行尾

Git * text=auto in the gitattributes file and line endings

基于此post: What is the purpose of `text=auto` in `.gitattributes` file? 如果 .gitattributes 文件中包含以下内容,则文本文件的行结尾将转换为 LF

* text=auto

我刚刚在本地存储库上对此进行了测试:

$ git add -A
warning: LF will be replaced by CRLF in [bla]/.gitattributes.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in [bla]/.gitignore.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in [bla]/README.md.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in [bla].csproj.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in 

但是那里说它将转换为 CRLF。在上面的 post 中,它表示它将转换为 LF,但在此测试中并非如此。

看来是:

* text=auto

将转换为基于 OS 的行结束类型(CRLF 表示 windows,LF 表示 linux)。但这不是这里描述的内容:

https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html

基于以下 comments/answers 似乎是由于以下原因产生的警告:

* text=auto

在 .gitattributes 文件中:

warning: LF will be replaced by CRLF in [bla]/README.md.
The file will have its original line endings in your working directory.

实际上意味着当你做一个 check-out (下次你从存储库检出一个文件到你的工作目录时)当前带有 LF 的文本文件 结尾将转换为 CRLF.

警告确实 NOT 解决了在 check-in 行将有 LF 结尾的问题这就是文档在这里所说的:

https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html

设置为字符串值"auto" 当文本设置为 "auto" 时,路径被标记为自动行尾规范化。如果 Git 确定内容是文本,则其行尾在签入时标准化为 LF。

这条消息有点混乱。

Git 会在您的文件与您当前的行尾转换设置不同时发出警告。此警告不是因为 Git 将 CRLF 放入存储库(它不是)- 出现此警告是因为 Git 将检出的文件与当前磁盘上的文件不同。

无论出于何种原因,您工作目录中的文件都有 Unix 风格的行结尾(或 Unix 和 Windows 风格的混合)。您应该能够使用十六进制编辑器看到这一点。例如,我有一个带有 Unix 样式行结尾的文件:

C:\Temp>hexdump /C foo
00000000  68 65 6c 6c 6f 21 0a                              |hello!.|
00000007

如果我将文件添加到我的存储库(使用 * text=autocore.autocrlf=true):

C:\Temp>git add foo
warning: LF will be replaced by CRLF in foo.
The file will have its original line endings in your working directory.

如 git 所示,我当前工作目录中的文件具有其原始(Unix 风格)行尾:

C:\Temp>hexdump /C foo
00000000  68 65 6c 6c 6f 21 0a                              |hello!.|
00000007   

但是存储库中的文件有 Unix 风格的行尾:

C:\Temp>git ls-files --stage
100644 4effa19f4f75f846c3229b9dbdbad14eff362f32 0       foo

C:\Temp>git cat-file blob 4effa19 | hexdump /C
00000000  68 65 6c 6c 6f 21 0a                              |hello!.|
00000007

但是如果我要求 git 创建文件内容 然后 它将创建一个不同的文件 - 一个带有 CRLF 行结尾的文件,这就是该警告实际指示的内容:

C:\Temp>del foo

C:\Temp>git checkout -f foo

C:\Temp>hexdump -C foo
00000000  68 65 6c 6c 6f 21 0d 0a                           |hello!..|
00000008

因此,此消息只是警告您,此文件的下一次签出将不会与您当前磁盘上的内容实际匹配。在这种情况下,这可能是无害的,但如果您要添加一个文件,其中行结束配置对它的匹配至关重要。