git 的正确 autocrlf 设置

proper autocrlf setting for git

我们有时会使用 SourceTree 作为客户端。 它可以处理各种行尾,但它使用的 diff 工具不能。 它将所有以 LF 结尾的行视为一行。

因此,我们使用 CRLF 将所有源代码保存在我们的存储库中。

我正在考虑安装 git 客户端。 我找不到 TEXT 或 AUTOCRLF 的正确设置。 好像他们都想 "normalize" 签入 LF 的文件。

我要 结帐时转换为 CRLF; 和 签入时转换为 CRLF; 要么 签入时什么都不做;

到目前为止我能找到的最好的是 -text:在签入或签出时什么也不做;

还有希望吗?

谢谢, 布拉德.

Git 的本机行尾设置是 LF,除了从源代码重新编译外,我不知道有什么方法可以改变这种行为。但是,您可以强制结帐使用 CRLF,这需要创建 .gitattributes 文件。例如:

# Force C# source files to be checked out using CRLF line endings,
# ignoring "core.eol" setting.
*.cs eol=crlf

# Don't apply CRLF conversion to PDF files, regardless of
# user settings including "core.autocrlf".
*.pdf -text

# All other files are subjected to the usual algorithm to determine
# whether a file is a binary file or a text file, respecting
# "core.eol" for all files detected as text files.
# "core.autocrlf", if set, will force the conversion to/from CRLF
# automatically as necessary for text files.
* text=auto

请注意,如果您创建了一个带有 LF 行尾的新文件(例如使用 Linux、Cygwin 或 Notepad++ 等默认为 LF 行尾的应用程序),您将在添加该文件时收到警告如果文件与 .gitattributes 文件中使用 eol=crlf 的模式匹配:

warning: LF will be replaced by CRLF in Example.cs.
The file will have its original line endings in your working directory.

当然,如果您稍后拉取新的更改,原来的 LF 行结尾将不再存在。

以下是各种 EOL/text 设置之间的交互列表:

  1. .gitattributes 文件
    • eol=...
      • 可能的值为 lfcrlf
      • 指定的文件始终被视为文本文件,并使用指定的行结尾检出。
      • 忽略 core.eolcore.autocrlf
    • text
      • 指定的文件始终被视为文本文件。
      • 尊重 core.eolcore.autocrlf
    • text=auto
      • 对指定的文件进行自动检测以确定它们是文本文件还是二进制文件。
      • 尊重 core.eolcore.autocrlf 文本文件。
      • 忽略二进制文件的 core.eolcore.autocrlf
    • -text
      • 指定的文件始终被视为二进制文件。
      • 忽略 core.eolcore.autocrlf,因为它们不适用于二进制文件。
  2. core.autocrlf配置设置
    • input: 任何签入的文本文件都将从 CRLF 转换为本机 LF。
    • true: 检查的任何文本文件 in/out 都经过 CRLF 转换。
    • 可能会被应用于 .gitattributes 中某些文件的 eol=... 规则覆盖。
  3. core.eol配置设置
    • native(默认):任何检出的文本文件都有本地行结尾(Windows 上的 CRLF,其他地方的 LF)
    • lf: 任何签出的文本文件都有 LF 行结尾
    • crlf: 任何签出的文本文件都有 CRLF 行结尾
    • 如果 core.autocrlf 设置为 true,此设置将被忽略。
    • 可能会被应用于 .gitattributes 中某些文件的 eol=... 规则覆盖。