33

有没有办法确定现有 git 存储库中的行尾?

如果我克隆现有存储库,如何确定创建者使用了什么 core.autocrlf?

我仍然不确定 core.autocrlf 的最佳设置是什么,例如在 windows 框中(因为有多种意见: Distributing git configuration with the codehttps://help.github.com/articles/dealing-with-line-结局

奖励问题:您能否在 Windows(使用标准工具)上确定回购是否在所有提交中具有混合行结尾(通过错误的 core.autocrlf 设置)?

4

4 回答 4

30

要检查存储库中实际提交的行尾(无论您的core.autocrlf设置如何),请尝试以下操作:

git grep -I --files-with-matches --perl-regexp '\r' HEAD

-I意味着不应查看二进制文件。)

于 2012-08-06T09:21:50.563 回答
5

有没有办法确定现有 git 存储库中的行尾?

我在“git status即使内容相同也显示文件已更改”中提到,这git ls-files --eol是一个好方法。(Git 2.8+)

让 Git 在索引和工作树中显示行尾以及有效的 text/eol 属性成为可能。

行尾(“eolinfo”)如下所示:

"-text"        binary (or with bare CR) file
"none"         text file without any EOL
"lf"           text file with LF
"crlf"         text file with CRLF
"mixed"        text file with mixed line endings.

例子:

i/none   w/none   attr/text=auto      t/t5100/empty
i/-text  w/-text  attr/-text          t/test-binary-2.png
i/lf     w/lf     attr/text eol=lf    t/t5100/rfc2047-info-0007
i/lf     w/crlf   attr/text eol=crlf  doit.bat
i/mixed  w/mixed  attr/               locale/XX.po

话虽如此:

正如我在您提到的“使用代码分发 git 配置”中解释的那样,我仍然会将该设置 ( core.autocrlf)保持为 ,并使用gitattributes 指令进行更细粒度的控制。falseeol

话虽如此,要检测混合行结尾:

  • 设置core.autocrlftrue
  • git clone你的回购
  • git diff: 如果在你的克隆之后差异是可见的......一些自动 eol 转换刚刚发生在工作树中。

2016 年更新(4 年后):一种更现代的检测 eol 变化的方法

 git -c color.diff.whitespace="red reverse" diff -R -- afile
于 2012-08-06T08:28:59.013 回答
3

最好的行尾做法在这里:https ://stackoverflow.com/a/10855862

要确定现有 git 存储库中的行尾:

  1. 设置core.autocrlffalse,因此在传输文件时不会更改文件结尾。
  2. git clone你的回购协议。在新目录中。
  3. 使用显示行尾的文本编辑器打开文件(例如 PHPStorm)。您应该打开多个文件,因为文件之间的行尾可能不同。

您无法确定创建者使用了什么 core.autocrlf,因为它是本地配置,除非 repo 有 .gitattributes 文件。

在 Windows 上,如果您不使用 .gitattributes,只需使用默认设置的 core.autocrlf true 即可。

于 2016-12-22T10:11:34.650 回答
1

有没有办法确定现有 git 存储库中的行尾?

git ls-files --eol

# for a given file:
git -c color.diff.whitespace="red reverse" diff -R -- afile

在 Windows 中,只需从命令提示符运行以下命令:

git config --list

这将列出所有 git 配置变量。

如果您想获取单独的配置设置(例如 core.autocrlf),请在 windows 命令提示符下运行以下命令:

git config --get core.autocrlf

这会给你一个“ true”或“ false”值。

如果您想更改此设置,请编辑C:\ProgramData\Git\config文件并将值从更改falsetrue

注意:这仅适用于 Windows 操作系统。

于 2019-05-27T16:39:45.860 回答