29

最近我在 .gitattributes 文件中发现了以下条目:

"* text=auto !eol"

做什么!eol

4

4 回答 4

21

Git 有 2 个处理行尾的属性:

  1. 文本

文档说:

此属性启用和控制行尾标准化。当一个文本文件被规范化时,它的行尾在存储库中被转换为 LF

这实际上意味着当您提交 repo 时,它会将行尾转换为 LF

  1. eol

文档说:

此属性设置要在工作目录中使用的特定行尾样式。它无需任何内容检查即可启用行尾规范化,从而有效地设置文本属性。

因此,虽然text属性会影响文件在 REPO 中的外观,但也会eol影响文件在工作目录中的外观。

现在,一个属性可以有 4 种状态:

设置无值
示例:* text

未设置的
示例:* -text

设置特定值
示例:* text=auto

未指定的
示例:* !text

所以,* text=auto !eol这意味着:

所有文件都将属性text设置为auto并且eol属性未指定。阅读文档我们发现这text=auto意味着您让 Git 决定文件是否为文本,如果是则将其规范化(将 repo 中的行尾设置为 LF)。

!eol表示该属性eol被显式设置为未指定。在这种情况下,它与根本不指定它相同,指示 Git 查看core.autocrlfcore.eol配置设置以了解如何处理工作目录中的行尾。请注意:

配置变量控制 Git将core.eol使用哪些行结尾来处理工作目录中的规范化文件;默认是使用您平台的本机行结尾,或者如果core.autocrlf设置了 CRLF。

但是您会!eol在以下情况下使用:

* text=auto eol=crlf
test.txt !eol

eol基本上将属性从 CRLF覆盖为未指定test.txt. 这意味着对于除 之外的所有文件test.txt,Git 将在结帐时将行尾转换为 CRLF。因为test.txtGit 将遵循core.autocrlfcore.eol配置设置,因此在任何给定系统上,行尾可能是 LF 或 CRLF。

于 2016-05-05T18:42:23.170 回答
6
* text=auto !eol

暗示:

  • 不会对二进制文件执行 EOL(行尾)转换。
  • 对于文本文件,EOL 在签出文件时转换为依赖于操作系统的 EOL(对于 Unix 转换为 LF,对于 Windows 转换为 CR+LF),并在签入时替换为 LF。
于 2014-01-23T08:25:37.053 回答
4

它基本上eol根据文档禁用:

有时您需要覆盖未指定状态路径的属性设置。这可以通过列出以感叹号 ! 为前缀的属性名称来完成。

eol执行以下操作:

此属性设置要在工作目录中使用的特定行尾样式。它无需任何内容检查即可启用行尾规范化,从而有效地设置文本属性。

于 2014-01-23T08:24:04.160 回答
2

精简版:

如果 Git 确定内容是文本,则在签入时将其行结尾规范化为 LF。还原某些嵌套的 .gitattributes 文件中的任何显式 eol 设置。

man gitattributes

   Each line in gitattributes file is of form:

       pattern attr1 attr2 ...

   Sometimes you would need to override an setting of an attribute for a path to
   Unspecified state. This can be done by listing the name of the attribute
   prefixed with an exclamation point !.

   text
       This attribute enables and controls end-of-line normalization. When a text
       file is normalized, its line endings are converted to LF in the
       repository. To control what line ending style is used in the working
       directory, use the eol attribute for a single file and the core.eol
       configuration variable for all text files.

       Set to string value "auto"
           When text is set to "auto", the path is marked for automatic
           end-of-line normalization. If Git decides that the content is text,
           its line endings are normalized to LF on checkin.

   eol
       This attribute sets a specific line-ending style to be used in the working
       directory. It enables end-of-line normalization without any content
       checks, effectively setting the text attribute.
于 2014-01-23T08:25:05.287 回答