Git * text=auto 在 gitattributes 文件和行尾

标签 git gitattributes

基于这篇文章: 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。在上面的帖子中,它说它将转换为 LF,但在此测试中并非如此。

看来:

* text=auto

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

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

根据以下评论/答案,似乎由此产生的警告:

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

实际上意味着当你做一个 checkout -out(下次你从版本库 checkout 一个文件到你的工作目录)当前以LF结尾的文本文件将是转换为具有 CRLF

该警告NOT 解决了在签到时in 行将以LF 结尾的问题,这就是文档在此处所说的内容:

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

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

最佳答案

这条消息有点令人困惑。

只要 Git 不打算按照您当前的行结束转换设置来回传输您的文件,Git 就会警告您。此警告不是因为 Git 将 CRLF 放入存储库(它不是)- 出现此警告是因为 Git 将 checkout 的文件与当前磁盘上的文件不同。

无论出于何种原因,您工作目录中的文件都有 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

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

关于Git * text=auto 在 gitattributes 文件和行尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33872793/

相关文章:

git - 无法在macOS上构建

git - 在 gitconfig 文件中转义别名命令

git - 如果两个结构相同的 git repos 从来没有任何共同的历史记录,它们可以 merge 吗?

git - `git` 显示克隆后更改的文件,没有任何其他操作

git - 我如何将 core.ignorecase 添加到 .gitattributes

git - 修改远程仓库中的 .git 文件夹

git - 由于 gitattributes eol 设置覆盖未暂存的提交

git - git log如何跨分支工作?

git - 如何确定某个提交的可达性?

c# - git中的值是什么意思?