Git:自制文件存在行尾问题



所以,我有一个新的存储库,我正在尝试在其中启动一个协作项目。我已经将.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 到 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 FilesGitcmdgit.exe" -c core.quotedpath=false add --ignore-errors -- tpCuat/CMakeLists.txt

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

此警告(或错误)的意思正是:您的文件中有一个换行符(n,ASCII 10),并且您有一个配置告诉 Git 它应该执行 CRLF 转换。 在这种情况下,这是您的.gitattributes

* text=auto

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

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

当您将裸换行符放入文件中但告诉 Git 进行 CRLF 转换时,它无法往返此操作。 考虑您是否有一些文件(图示的行尾):

line onern
line tworn
line three accidentally ends with a newline!n
line fourrn

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

line onen
line twon
line three accidentally ends with a newline!n
line fourn

当它再次检查时:

line onern
line tworn
line three accidentally ends with a newline!rn
line fourrn

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

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

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

最新更新