2

所以,我有这个新的仓库,我正在尝试开始一个协作项目。我已经向它推送.gitignore.gitattributes(处理自动crlf)文件。

我的.gitattributes文件是:

# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto

# Explicitly declare text files you want to always be normalized and converted
# to native line endings on checkout.
*.c text
*.h text

# Declare files that will always have CRLF line endings on checkout.
*.sln text eol=crlf

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary
*.mdj binary

我在 GitHub 上创建了一个存储库,并通过 SourceTree 将其克隆到我的 PC 上。现在,我正在尝试在其中创建一个新的 CLion 项目,但是当我尝试 添加 CMakemain.c提交文件时,我收到 LF to CRLF 错误:

The following problems have occurred when adding the files:
fatal: LF would be replaced by CRLF in tpCuat/CMakeLists.txt
 during executing git "C:\Program Files\Git\cmd\git.exe" -c core.quotedpath=false add --ignore-errors -- tpCuat/CMakeLists.txt

问题是,这些文件是我在 Windows 中创建的(实际上是 CLion),所以我不明白为什么会出现这个错误。

4

1 回答 1

2

此警告(或错误)的确切含义是:您的文件中有一个换行符(\nASCII 10),并且您有一个配置告诉 Git 它应该进行 CRLF 转换。在这种情况下,它是你的.gitattributes

* text=auto

这是一个问题,因为 Git 告诉您它不能保证您放入文件就是您以后取出的文件。

当您将此文件添加到存储库 ( git add) 时,该text=auto属性会告诉 Git 在将文件存储在存储库中时将文件中的所有 CRLF ( \r\n,ASCII 13 后跟 ASCII 10) 转换为裸换行符 ( \n, ASCII 10)。当 Git 随后尝试将文件放入磁盘 ( git checkout) 时,它将在写入文件时将文件中的所有换行符 ( \n) 转换为 CRLF ( \r\n)。

当您将一个简单的换行符放入文件但告诉 Git 进行 CRLF 转换时,它无法往返。考虑您是否有一些文件(说明了行尾):

line one\r\n
line two\r\n
line three accidentally ends with a newline!\n
line four\r\n

现在,当 Git 将其添加到存储库时,它将进行 CRLF 转换并存储:

line one\n
line two\n
line three accidentally ends with a newline!\n
line four\n

当它再次检查时:

line one\r\n
line two\r\n
line three accidentally ends with a newline!\r\n
line four\r\n

请注意第三行现在是如何以 CRLF ( ) 结尾的,\r\n而它不在原始文件中? 就是 Git 在这里警告你的。它告诉你,它不可能准确地把你投入的东西还给你。

如果这对您来说是个问题,那么您应该打开,core.safecrlf以便在发生这种情况时 Git 会出错并要求您修复行尾问题。否则,如果您不在乎,可以放心地忽略此警告。

为什么 CLion 会做一些愚蠢的事情,比如将一个裸换行符放入您的文件中?好吧,这完全是另一个问题!

于 2017-05-18T09:05:06.490 回答