使 Git“LF 将被 CRLF 替换”警告消失

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1598260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:14:31  来源:igfitidea点击:

Make Git "LF will be replaced by CRLF" warnings go away

git

提问by corydoras

I have setup Git so it doesn't commit inconsistent line endings. The problem with that is a whole pile of files appear modified even though they are not. What do I type to make these files have the line endings fixed on the local side?

我已经设置了 Git,所以它不会提交不一致的行结尾。问题是一大堆文件看起来被修改了,即使它们没有。我输入什么来使这些文件的行尾固定在本地?

# git checkout dev
M   src/au/policy/dao/EmailQueue.java
M   src/au/policy/dao/EmailQueueFactory.java
M   src/au/policy/dao/PolicyPublisher.java
Already on 'dev'

# git diff
warning: LF will be replaced by CRLF in src/au/policy/dao/EmailQueue.java
warning: LF will be replaced by CRLF in src/au/policy/dao/EmailQueueFactory.java
warning: LF will be replaced by CRLF in src/au/policy/dao/PolicyPublisher.java

This is what I added to my git config file which seems to do what I intended aside from this issue:

这是我添加到我的 git 配置文件中的内容,除了这个问题之外,它似乎做了我想要的:

autocrlf = true

采纳答案by CB Bailey

You can just delete and re-checkout the offending files from the index like this:

您可以像这样从索引中删除并重新检出有问题的文件:

rm <files>
git checkout -- <files>

Or, if they are the only modified files (be careful with this command), you can script it like this:

或者,如果它们是唯一修改过的文件(小心使用此命令),您可以像这样编写脚本:

git diff --name-only --diff-filter=M | xargs rm --
git checkout -- .

On a GNU system you can use a slightly safer pipe, but you don't appear to have spaces or other delimiting characters in your filenames in any case.

在 GNU 系统上,您可以使用稍微安全一些的管道,但在任何情况下,您的文件名中似乎都没有空格或其他分隔字符。

git diff -z --name-only --diff-filter=M | xargs -0 rm --

回答by Jakub Nar?bski

This might happen if you change core.autocrlfconfig variable (if I understand your problem correctly).

如果您更改core.autocrlf配置变量(如果我正确理解您的问题),则可能会发生这种情况。

If you are at clean state, i.e. just after commit, and you don't have uncomitted changes, forced re-checkout and removing index should do the trick:

如果您处于干净状态,即在提交之后,并且您没有未提交的更改,则强制重新签出并删除索引应该可以解决问题:

The below command git reset --hard HEADwill make your current branch to point to the latest commit and all uncommitted code will be lost. Make sure to commit the code or take the backup

下面的命令git reset --hard HEAD将使您当前的分支指向最新的提交,所有未提交的代码都将丢失。确保提交代码或进行备份

$ rm .git/index
$ git reset --hard HEAD

That, I think, would sync both working area files, and the index (staging area) to follow crlf settings.

我认为,这将同步工作区文件和索引(暂存区)以遵循 crlf 设置。

回答by Sonique

I had this problem when creating new Xcode project. My solution for this problem:

我在创建新的 Xcode 项目时遇到了这个问题。我对这个问题的解决方案:

In terminal write

在终端写

$: git config --global --edit

Then in git config file change safecrlfto false. My settings:

然后在 git 配置文件中将 safecrlf更改为false。我的设置:

[core]
    autocrlf = input
    safecrlf = false

I know git have cmd line tools for this but they don't work for me. And then Xcode create git repos without any problem.

我知道 git 有用于此的 cmd 行工具,但它们对我不起作用。然后Xcode创建git repos没有任何问题。

回答by Bob Aman

Only thing I can think of is to check if core.safecrlfis set to warn.

我唯一能想到的就是检查是否core.safecrlf设置为warn.

git config --get core.safecrlf

git config --get core.safecrlf

I thinkpossible values are true, false, and warn. I believe that setting to falsewill resolve the warning, though it may not be a good idea.

认为可能的值是truefalsewarn。我相信设置为false将解决警告,尽管这可能不是一个好主意。

回答by Alexander Miller

Try this, it worked for me:

试试这个,它对我有用:

cd src/au/policy/dao
dos2unix

If there are other files in that folder, then you'll want to break it up into the following (otherwise it will try to do it on every file in any subdirectories, which may take a while):

如果该文件夹中有其他文件,那么您需要将其分解为以下内容(否则它将尝试在任何子目录中的每个文件上执行此操作,这可能需要一段时间):

cd src/au/policy/dao
dos2unix EmailQueue.java
dos2unix EmailQueueFactory.java
dos2unix PolicyPublisher.java

It ran really quick on my machine and fixed all of the line endings, and it's a little simpler and easier than some of these other fixes.

它在我的机器上运行得非常快,并修复了所有的行尾,而且比其他一些修复更简单、更容易。

回答by Geoffrey VL

Note that note all of the above fixes may work for you, for example you might have received the code through a simple file transfer. You can accept each warning by pressing ENTER, but on on large repos that can take ages. Another way to ignore the conversion on code that has already been checked-out and edited is to create a patch file:

请注意,请注意上述所有修复程序可能对您有用,例如您可能已经通过简单的文件传输收到了代码。您可以按 ENTER 接受每个警告,但在可能需要很长时间的大型存储库上。另一种忽略已签出和编辑代码的转换的方法是创建补丁文件:

git diff > changes.patch