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

LF将被CRLF取代

这种转变的后果是什么?


当前回答

在谈到这个话题时,GitHub上一篇关于行尾的文章经常被提到。

我个人使用经常推荐的核心的经验。自专制的配置设置非常复杂。

我在Cygwin中使用Windows,在不同的时间处理Windows和Unix项目。甚至我的Windows项目有时也使用Bash shell脚本,这需要Unix (LF)行结束符。

使用GitHub推荐的核心。如果我签出一个Unix项目(它在Cygwin上工作得很好-或者我正在为一个我在Linux服务器上使用的项目做贡献),文本文件会用Windows (CRLF)行结束符签出,这会产生问题。

基本上,对于像我这样的混合环境,设置全局核心。在某些情况下,任意选择都不能很好地工作。这个选项可以在本地(存储库)Git配置上设置,但即使是这样,对于同时包含Windows和unix相关内容的项目(例如,我有一个带有一些Bash实用程序脚本的Windows项目)也不够好。

我发现最好的选择是创建每个存储库的.gitattributes文件。GitHub的文章提到了它。

文章中的例子:

# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto

# Explicitly declare text files you want to always be normalized and converted
# to native line endings on checkout.
*.c text
*.h text

# Declare files that will always have CRLF line endings on checkout.
*.sln text eol=crlf

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary

在我的一个项目的存储库:

* text=auto

*.txt         text eol=lf
*.xml         text eol=lf
*.json        text eol=lf
*.properties  text eol=lf
*.conf        text eol=lf

*.awk  text eol=lf
*.sed  text eol=lf
*.sh   text eol=lf

*.png  binary
*.jpg  binary

*.p12  binary

要设置的东西更多一些,但是每个项目都要设置一次,任何操作系统上的任何贡献者在处理这个项目时都应该不会遇到行结束符的问题。

其他回答

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”值的更新信息。

我不太了解Windows上的Git,但是……

在我看来,Git正在转换返回格式以匹配正在运行的平台(Windows)。CRLF是Windows上的默认返回格式,而LF是大多数其他操作系统的默认返回格式。

当代码被移动到另一个系统时,返回格式可能会得到适当的调整。我还认为Git足够聪明,可以保持二进制文件的完整性,而不是试图在JPEG文件中将lf转换为crlf。

总而言之,您可能不需要为这种转换担心太多。但是,如果您将项目归档为tarball,其他编码员可能会喜欢使用LF行终止符而不是CRLF。取决于你有多关心(取决于你不使用记事本),你可能想要设置Git使用LF返回,如果你可以的话:)

附录:CR为ASCII码13,LF为ASCII码10。因此,CRLF是两个字节,而LF是一个字节。

我也有同样的问题,做git添加。&& git重置恢复所有行结束正确。

OP的问题是与windows相关的,我无法使用其他的目录,甚至无法在notepad++中运行文件,因为管理员无法工作…

所以不得不走这条路:

cd "C:\Program Files (x86)\Git\etc"
git config --global core.autocrlf false

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

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

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