在一台Windows机器上,我使用git add添加了一些文件。 我收到警告说:

LF将被CRLF取代

这种转变的后果是什么?


当前回答

在Vim中,打开文件(例如::e YOURFILEENTER),然后

:set noendofline binary
:wq

其他回答

Git有三种模式来处理行结束符:

# This command will print "true" or "false" or "input"
git config core.autocrlf

您可以通过向上面的命令行添加一个额外的true或false参数来设置要使用的模式。

If core.autocrlf is set to true, that means that any time you add a file to the Git repository that Git thinks is a text file, it will turn all CRLF line endings to just LF before it stores it in the commit. Whenever you git checkout something, all text files automatically will have their LF line endings converted to CRLF endings. This allows development of a project across platforms that use different line-ending styles without commits being very noisy, because each editor changes the line ending style as the line ending style is always consistently LF.

The side effect of this convenient conversion, and this is what the warning you're seeing is about, is that if a text file you authored originally had LF endings instead of CRLF, it will be stored with LF as usual, but when checked out later it will have CRLF endings. For normal text files this is usually just fine. The warning is a "for your information" in this case, but in case Git incorrectly assesses a binary file to be a text file, it is an important warning, because Git would then be corrupting your binary file.

如果核心。将selflf设置为false,则不会执行任何行结束转换,因此文本文件将按原样检入。这通常是可行的,只要你所有的开发人员都在Linux或Windows上。但根据我的经验,我仍然倾向于得到带有混合行结束符的文本文件,最终导致问题。

作为一名Windows开发人员,我个人倾向于让这个设置打开。

请参阅git-config以获得包含“input”值的更新信息。

我也有这个问题。

SVN不执行任何行结束转换,因此提交文件时保留CRLF行结束。如果您使用git-svn将项目放入git中,那么CRLF结束符将持续到git存储库中,这不是git希望自己处于的状态——默认情况是只检入unix/linux (LF)行结束符。

当您在windows上签出文件时,selflf转换会保持文件不变(因为它们已经具有当前平台的正确结尾),但是,决定签入文件是否存在差异的进程会在比较之前执行反向转换,从而将它认为是签出文件中的LF与存储库中的意外CRLF进行比较。

在我看来,你的选择是

Re-import your code into a new git repository without using git-svn, this will mean line endings are converted in the intial git commit --all Set autocrlf to false, and ignore the fact that the line endings are not in git's preferred style Check out your files with autocrlf off, fix all the line endings, check everything back in, and turn it back on again. Rewrite your repository's history so that the original commit no longer contains the CRLF that git wasn't expecting. (The usual caveats about history rewriting apply)


注:如果你选择选项2,那么我的经验是,一些辅助工具(rebase, patch等)不能处理CRLF文件,你迟早会得到混合了CRLF和LF(不一致的行尾)的文件。我不知道有什么办法能两全其美。

确保您已经安装了最新版本的Git

我在之前的回答中,git配置核心。在使用Git(2.7.1版)时,自专制错误,但它不工作。

然后,当升级git(从2.7.1到2.20.1)时,它就可以工作了。

在Vim中,打开文件(例如::e YOURFILEENTER),然后

:set noendofline binary
:wq

unix2dos和dos2unix都可以在Windows Git Bash上使用。可以使用以下命令进行UNIX (LF)→DOS (CRLF)转换。因此,您将不会得到警告。

unix2dos filename

or

dos2unix -D filename

但是,不要在任何现有的CRLF文件上运行这个命令,因为这样每隔两行就会得到空换行符。

dos2unix -D文件名不适用于所有操作系统。请检查此链接的兼容性。

如果出于某种原因你需要强制执行命令,那么使用——force。如果它表示无效,则使用-f。