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

LF将被CRLF取代

这种转变的后果是什么?


当前回答

其他答案对于一般概念来说是非常棒的。我遇到了一个问题,在更新警告后,仍然发生在现有的存储库,在以前的设置中提交。

加上——renoralize有帮助,例如:

Git添加——重新规范化。

从文档中可以看到:

将“清洁”进程新应用于所有跟踪文件,以强制执行 将它们再次添加到索引中。这在更改后很有用 核心。自专制配置或文本属性,以纠正 添加了错误的CRLF/LF行结束符的文件。这个选项意味着-u。”

其他回答

CR和LF是一组特殊的字符,用于帮助格式化代码。

CR (/r)将光标放在一行的开头,但不创建新的行。这就是macOS的工作原理。

LF (/n)创建了一个新行,但它不会将光标放在该行的开头。光标停留在最后一行的末尾。这就是Unix和Linux的工作原理。

CRLF (/r/f)创建新行,并将光标放在新行开头。这就是我们在Windows操作系统中看到的情况。

总结:

Lf(换行)

代表换行 用/n表示 在代码中创建新行 ASCII码是10。 用于Unix和其他基于它的操作系统。

Cr(车厢返程)

代表回车 用/r表示 将光标放在行首。 ASCII码是13。 macOS及其前身使用。

CRLF(回车换行)

代表回车和换行 用/n/r表示 创建新行,并将光标放在新行开头。 LF的ASCII码是10,CR的ASCII码是13。 主要用于Windows操作系统。

Git默认使用LF。因此,当我们在Windows上使用Git时,它会抛出一个像“CRLF将被LF取代”这样的警告,并自动将所有CRLF转换为LF,因此代码变得兼容。

NB:别担心……不要把这看作是警告,而应该把它看作是通知消息。

我也有这个问题。

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(不一致的行尾)的文件。我不知道有什么办法能两全其美。

在GNU/Linux shell提示符中,dos2unix和unix2dos命令允许您轻松地转换/格式化来自MS Windows的文件。

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

在谈到这个话题时,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

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