如何关闭Git警告“LF将被CRLF替换”?

18

我正在使用 Windows,但也可能在 Unix 上工作,因此不需要存储 Windows 行尾符。我只想抑制警告。

我找到了这些相关的 Stack Overflow 问题:

我尝试了以下方法:

git config core.whitespace cr-at-eol false<Br>
git config core.whitespace cr-at-eol true<br>
git config core.whitespace cr-at-eol nowarn

但这些似乎没有起到任何作用。有人知道如何关闭警告吗?


4个回答

9

我通常在 .git/config 文件中使用 autocrlf=true 来应对大多数Windows环境下的情况。根据新的源文件会偶尔出现警告。

如果你有一些特殊的文件不遵循这个规则,可以为它们单独设置一个 .gitattributes 文件。例如,我有些Matlab文件的设置为 *.m eol=lf


2
为了向像我这样的完全不懂git的新手解释:文件“config”位于.git目录中。在Windows上,该目录的“隐藏”属性已设置。 - Bob Stine
1
这不会关闭警告。它将更改Git的自动换行转换,以检查文本文件是否带有LF并将其检出为CLRF行尾。这种设置每个存储库通常是不寻常的。相反,当您检入带有CLRF的文件时,您将收到警告,因为它们将被LF替换。 - CervEd

6

我使用了以下方法:

The git config core.autocrlf command is used to change how Git handles line endings. It takes a single argument.

On Windows, you simply pass true to the configuration. For example:

$ git config --global core.autocrlf true    
# Configure Git on Windows to properly handle line endings

You can also provide a special --global flag, which makes Git use the same settings for line endings across every local Git repository on your computer.

After you've set the core.autocrlf option and committed a .gitattributes file, you may find that Git wants to commit files that you have not modified. At this point, Git is eager to change the line endings of every file for you.

The best way to automatically configure your repository's line endings is to first backup your files with Git, delete every file in your repository (except the .git directory), and then restore the files all at once. 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/

本文介绍如何在 GitHub 中处理不同行尾符的文件。当在不同的操作系统(如 Windows、macOS 或 Linux)之间共享代码时,文件中使用的行尾符可能会导致问题。GitHub 为解决这些问题提供了多种解决方案,包括自动转换行尾符以及指定应该使用哪种行尾符。

1
这个答案也不会关闭警告,它只是改变了检入和检出文件的行尾处理方式。这是正确的答案 https://dev59.com/g2w15IYBdhLWcg3wntAQ#14640908 - CervEd

3
您可以使用以下命令关闭警告: git config --global core.safecrlf false (这只会关闭警告,而不是功能本身。)

https://dev59.com/g2w15IYBdhLWcg3wntAQ#14640908


问题是如何在使用 autocrlf 设置行尾转换时,关闭检入 Git 时生成的警告。
这些警告表示行尾将不会被保留。也就是说,您有一个处于特定状态的文件,您正在将其检入 Git,但是当您在其他时间检出它时,不应该期望它处于完全相同的状态。或者,如果别人检出它。
如果更改 core.autoclrf 的设置,则只是更改要将文件作为何种格式检出的设置,并且随之翻转警告为“CRLF 将被 LF 替换”。除非将其更改为 input,否则在大多数情况下会使事情变得混乱。
行尾是否重要取决于项目,并且应该使用 .gitattributes 在整个项目范围内进行设置。

1
我很惊讶这个答案没有得到更多的赞同,因为它是唯一一个正确回答原问题的答案。core.autocrlf设置关注的是如何处理行尾,这与被问及的问题不同,实际上是core.safecrlf设置来解决警告本身的问题。 - Derecho

2
我也在Windows上进行开发,但由于我们的服务器是基于Linux运行的,所有的代码都必须基于Linux,因此我更喜欢将所有文件从CRLF更改为LF以保持一致。
从git命令行:
git config --global core.autocrlf false

我正在使用IntelliJ IDEA,所以很容易:
1)文件 -> 设置 -> 编辑器 -> 代码样式: (任何新文件都会被创建)
a. 方案: 默认
b. 行分隔符: Unix和OS X (\n)
2)标记项目根目录 -> 文件 -> 行分隔符 -> LF Unix和OS X (\n) (对于现有文件)
备注: 您还可以使用像dos2unix.exe或其他脚本的应用程序。
然后我使用命令行完成了这个操作:(您也可以从IDEA中完成此操作)
git commit -m "bla bla"
git add .
git push origin master

从那时起,我就没有收到那些警告了。

1
此答案也会关闭警告。它会改变Git对检入/检出文件的换行符处理方式。 - CervEd

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