使用Git,我如何关闭“LF将被替换为CRLF”的警告?

210

使用Git时,当使用autocrlf=true标志时,仍会在更改行尾时发出警告。

我理解警告的含义以及如何关闭行尾标志,但我该如何关闭警告本身?


5
这里所有的答案都已经过时了——因为git引入了gitattributes。safecrlf是你的朋友,而autocrlf不是!请参见我的答案 - Rusi
6个回答

365
你可以通过以下方式关闭警告:
git config --global core.safecrlf false

(这只会关闭警告,而不是功能本身。)


2
关闭警告会防止git将lf替换为crlf吗?@chronial - aidonsnous
12
git文档 中可以得知,如果将 core.safecrlf 设置为 "true" 或 "warn",git 会验证当前的 core.autocrlf 设置下转换是否可逆。对于 "true",git 会拒绝不可逆的转换;对于 "warn",git 只会打印一个警告信息但会接受不可逆的转换。如果您不需要拒绝不可逆的转换,则将 core.safecrlf 设置为 false 可以抑制警告,但仍然会自动转换。 - Daynil

9
你应该使用core.autocrlf inputcore.eol input。或者完全不要让git改变行尾,使用autocrlf false,并通过core.whitespace cr-at-eol消除差异中的crlfs高亮显示。
希望这能帮到你。

通常情况下,你希望你的BAT脚本以CRLF结尾并提交,而你的SH脚本以LF结尾。 - Sandburg

3
我使用了这种方法:

Save your current files in Git, so that none of your work is lost.

git add . -u
git commit -m "Saving files before refreshing line endings"

Remove every file from Git's index.

git rm --cached -r .

Rewrite the Git index to pick up all the new line endings.

git reset --hard

Add all your changed files back, and prepare them for a commit. This is your chance to inspect which files, if any, were unchanged.

git add .
# It is perfectly safe to see a lot of messages here that read
# "warning: CRLF will be replaced by LF in file."

Commit the changes to your repository.

git commit -m "Normalize all the line endings"

https://help.github.com/articles/dealing-with-line-endings/


13
我相信原帖作者希望不再看到那些警告。并非将所有换行符都规范化。 - Mike Cluck
git rm --cached -r . && git reset --hard 看起来可以解决问题,谢谢。 - Shanimal

0

您正在寻找 core.whitespace 选项(有关详细信息,请参见 git config --help)。

您可以这样设置此选项:

$ git config core.whitespace cr-at-eol

0

设置 "core.safecrlf false" 可以正常工作。但是,当我将值更改为'true'时,输出从'warning'更改为'fatal',如下所示。

$ git add -A
warning: LF will be replaced by CRLF in .gitignore.
The file will have its original line endings in your working directory

$ git config --global core.safecrlf false

$ git reset

$ git config --global core.safecrlf true

$ git add -A
fatal: LF would be replaced by CRLF in .gitignore

$

在最近的git中,最好使用gitattributes而不是autocrlf。请参见我的答案。所有答案(在此问题上)都已过时。 - Rusi

0
有趣的是,我已经按照这里所解释的方法应用了两个配置,并且我的.gitconfig文件包含了这两行:
[core]
       autocrlf = false
       whitespace = cr-at-eol

然而我收到了警告。 现在只是为了尝试,我注释掉了这两行代码,结果这个警告竟然消失了。 不过我真的不知道当时为什么要加上这两行代码...


你的代码仓库设置不同吗? - CervEd
抱歉回复晚了... 我没有我的配置文件了,但我现在使用一个类似的,除了这两行之外,没有任何相关的内容。 - Jean-Michel Bernard
因为如果你的 .git/config 中有其他设置,那些设置将会覆盖你的全局设置。 - CervEd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接