【问题标题】:proper autocrlf setting for gitgit的正确autocrlf设置
【发布时间】:2015-10-02 21:02:38
【问题描述】:

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

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

我正在考虑安装一个 git 客户端。 我找不到 TEXT 或 AUTOCRLF 的正确设置。 似乎他们都想在签入到 LF 时将文件“规范化”。

我想 结帐时转换为 CRLF; 并且要么 签入时转换为 CRLF; 或者 签到时什么都不做;

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

还有希望吗?

谢谢, 布拉德。

【问题讨论】:

    标签: git eol


    【解决方案1】:

    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 或默认为 LF 行结尾的 Notepad++ 等应用程序),如果将该文件添加到存储库中,则会收到警告匹配.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/文本设置之间的交互列表:

    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: 任何签入/签出的文本文件都会进行 CRLF 转换。
      • 可能会被应用于.gitattributes 中某些文件的eol=... 规则覆盖。
    3. core.eol配置设置
      • native(默认):检出的任何文本文件都有本机行结尾(Windows 上为 CRLF,其他地方为 LF)
      • lf: 任何检出的文本文件都有 LF 行结尾
      • crlf: 任何检出的文本文件都有 CRLF 行结尾
      • 如果 core.autocrlf 设置为 true,则忽略此设置。
      • 可能会被应用于.gitattributes 中某些文件的eol=... 规则覆盖。

    【讨论】:

      猜你喜欢
      • 2016-05-08
      • 2017-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-04
      • 2013-03-19
      • 1970-01-01
      • 2012-06-15
      相关资源
      最近更新 更多