在一台Windows机器上,我使用git add添加了一些文件。 我收到警告说:
LF将被CRLF取代
这种转变的后果是什么?
在一台Windows机器上,我使用git add添加了一些文件。 我收到警告说:
LF将被CRLF取代
这种转变的后果是什么?
当前回答
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”值的更新信息。
其他回答
在GNU/Linux shell提示符中,dos2unix和unix2dos命令允许您轻松地转换/格式化来自MS Windows的文件。
从~/。gitattributes文件,
* text = auto
将防止Git首先检查行结束符。
我也有同样的问题,做git添加。&& git重置恢复所有行结束正确。
确保您已经安装了最新版本的Git
我在之前的回答中,git配置核心。在使用Git(2.7.1版)时,自专制错误,但它不工作。
然后,当升级git(从2.7.1到2.20.1)时,它就可以工作了。
我认为Basiloungas的答案很接近,但已经过时了(至少在Mac上)。
打开~/。并将safecrlf设置为false:
[core]
autocrlf = input
safecrlf = false
*显然会使它忽略行char的结尾(它对我来说是有效的)。