git*text = auto在gitattributes文件和行结尾中

u12*_*123 7 git gitattributes

基于这篇文章: `.gitattributes`文件中`text = auto`的目的是什么?如果.gitattributes文件中包含以下内容,则 行结尾将转换为LF以用于文本文件:

* text=auto
Run Code Online (Sandbox Code Playgroud)

我刚刚在本地存储库上测试过:

$ 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 
Run Code Online (Sandbox Code Playgroud)

但它说它将转换为CRLF.在上面的帖子中,它表示它将转换为LF,而在此测试中并非如此.

所以似乎:

* text=auto
Run Code Online (Sandbox Code Playgroud)

将转换为基于操作系统的行结束类型(Windows的CRLF和Linux的LF).但这不是这里描述的内容:

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

根据以下评论/答案,似乎有这样的警告:

* text=auto
Run Code Online (Sandbox Code Playgroud)

在.gitattributes文件中:

warning: LF will be replaced by CRLF in [bla]/README.md.
The file will have its original line endings in your working directory.
Run Code Online (Sandbox Code Playgroud)

实际上意味着,当你做一个结算(下一次你检出从库中的文件到你的工作目录)目前的文本文件与LF结局将被转换为具有CRLF.

该警告没有解决,关于入住线将有LF结局是什么文件说,在这里:

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

设置为字符串值"auto"当文本设置为"auto"时,路径将标记为自动行结束标准化.如果Git决定内容是文本,则其行结尾在签入时标准化为LF.

Edw*_*son 5

这条消息有点令人困惑.

只要不使用当前行结束转换设置对文件进行往返,Git就会发出警告.这个警告不是因为Git会将CRLF放入存储库(事实并非如此) - 这个警告的出现是因为Git要检出的文件与当前磁盘上的文件不同.

无论出于何种原因,工作目录中的文件都具有Unix样式行结尾(或Unix和Windows风格的混合).您应该可以使用十六进制编辑器查看此内容.例如,我有一个带有Unix样式行结尾的文件:

C:\Temp>hexdump /C foo
00000000  68 65 6c 6c 6f 21 0a                              |hello!.|
00000007
Run Code Online (Sandbox Code Playgroud)

如果我将文件添加到我的存储库(使用* 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.
Run Code Online (Sandbox Code Playgroud)

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

C:\Temp>hexdump /C foo
00000000  68 65 6c 6c 6f 21 0a                              |hello!.|
00000007   
Run Code Online (Sandbox Code Playgroud)

但是存储库中的文件有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
Run Code Online (Sandbox Code Playgroud)

但是,如果我要求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
Run Code Online (Sandbox Code Playgroud)

因此,此消息只是警告您,此文件的下一次检出实际上与您当前磁盘上的内容匹配.在这种情况下,这可能是无害的,但如果您要添加一个文件,其中行结束配置是关键的,那么这将是非常糟糕的.