在一台Windows机器上,我使用git add添加了一些文件。 我收到警告说:
LF将被CRLF取代
这种转变的后果是什么?
在一台Windows机器上,我使用git add添加了一些文件。 我收到警告说:
LF将被CRLF取代
这种转变的后果是什么?
当前回答
git config core.autocrlf false
其他回答
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”值的更新信息。
在Vim中,打开文件(例如::e YOURFILEENTER),然后
:set noendofline binary
:wq
如果您已经签出了代码,那么文件就已经建立了索引。在更改Git设置后,运行以下命令:
git config --global core.autocrlf input
您应该使用
git rm --cached -r .
并重写Git索引
git reset --hard
注意:这将删除您的本地更改。在你这么做之前考虑把它们藏起来。
来源:配置Git处理行结束符
如何使Git忽略不同的行结束
http://www.rtuin.nl/2013/02/how-to-make-git-ignore-different-line-endings/(不工作)
您可以完全禁用CRLF行为,或者通过更改.gitattributes文件中的条目来禁用每个文件类型。在我的例子中,我写的是: crlf 这告诉Git忽略所有文件的行结束符。并且不会更改工作目录中的文件。即使你有核心。自专制设置为true, false或输入。
echo "* -crlf" > .gitattributes
在单独提交时执行此操作,否则当您进行单个更改时,Git可能仍然会看到整个文件被修改(取决于您是否更改了selflf选项)。
这个真的有用。Git将尊重混合行结束项目中的行结束符,而不会就此警告您。
Windows中的大多数工具也接受文本文件中的简单LF。例如,你可以在一个名为'的文件中控制Visual Studio的行为。Editorconfig '使用以下示例内容(部分):
indent_style = space
indent_size = 2
end_of_line = lf <<====
charset = utf-8
只有原来的Windows记事本不能使用LF,但有一些更合适的简单编辑器工具可用!
因此你也应该在Windows的文本文件中使用LF。这是我的信息,强烈推荐!没有任何理由在窗口中使用CRLF !
(同样的讨论是在C/ c++中使用\ in include路径。这是牛的粪便。使用#include <pathTo/myheader.h> +斜杠!它是C/++标准,所有微软编译器都支持它)。
因此Git的正确设置是:
git config core.autocrlf false
我想说的是:忘记dos2unix和unix2dos这类思维陈旧的程序吧。在您的团队中阐明LF适合在Windows下使用。