【问题标题】:Where should I place my global 'gitattributes' file?我应该把我的全局“gitattributes”文件放在哪里?
【发布时间】:2015-03-17 14:02:00
【问题描述】:

我认为(尽管缺乏文档)a way 可以全局设置Git attributes;但我不清楚在哪里放置必要的gitattributes 文件。 instructions 说他们属于

$(prefix)/etc/gitattributes

但是$(prefix) 在哪里?特别是对于 OS X(在 /usr/local/git/bin/git 中使用 Git),它会在哪里?或者(或另外)会~/.gitattributeswork

【问题讨论】:

  • 在 Git 上下文中,“全局”通常表示“用户级”;换句话说,global 设置会影响 one 特定用户的所有存储库。相比之下,system-wide 设置会影响机器的所有 用户的所有存储库。您对哪个级别感兴趣?用户级还是系统级?
  • @Jubobs:两者(即,放置一个与另一个的位置;似乎也有特定于 Git 版本的位置,如果一个安装了多个 Git,就像我一样);但主要针对用户。

标签: git unix path gitattributes


【解决方案1】:

全局与系统范围的设置

您问题的术语有些含糊不清。 在 Git 上下文中,“全局”通常表示“用户级别”;换句话说,global 设置会影响 one 特定用户(活动用户)的所有存储库。相比之下,系统范围 设置会影响机器的所有 用户的所有存储库。

存储库级 gitattributes

(我只是为了完整性而提到这一点。)

根据relevant section of the Pro Git book

如果您希望仅影响单个存储库(即,将属性分配给特定于该存储库的一个用户工作流的文件),则应将属性放置在 $GIT_DIR/info/attributes 文件中。

$GIT_DIR 通常会扩展为 <path-to-repo-root-directory>/.git

全局(用户级)gitattributes

根据relevant section of the Pro Git book

应该影响单个用户的所有存储库的属性应该放在由core.attributesfile 配置选项[...] 指定的文件中。它的默认值为$XDG_CONFIG_HOME/git/attributes。如果$XDG_CONFIG_HOME 未设置或为空,则使用$HOME/.config/git/attributes 代替。

你也可以运行以下命令,

git config --global core.attributesfile <path>

将 Git 指向您的全局 gitattributes 文件的自定义路径 &lt;path&gt;,例如~/.gitattributes.

系统范围的 git 属性

根据relevant section of the Pro Git book

系统上所有用户的属性都应该放在$(prefix)/etc/gitattributes 文件中。

这自然引出了问题:

[...]但是$(prefix)在哪里?

请参阅What is $(prefix) on $(prefix)/etc/gitconfig? 以获得答案。除非您为 prefix 分配了一个自定义的非空值,否则 $(prefix) 默认扩展为空;因此,您的系统范围内的 gitattributes 文件应位于 /etc/

【讨论】:

  • 我有一个/usr/local/git/etc 目录,它似乎受到gitattributes 的尊重(至少受到/usr/local/git/bin/git 的尊重),但没有/etc/git。还有一个 ~/.config/git 目录,其中包含 GitHub 似乎已生成的忽略)。我不清楚所有这些是如何组合在一起的。
  • @raxacoricofallapatorius 在 GitHub 安装期间用于编译 Git 的前缀可能是 /usr/local/git/;如果是这样,/usr/local/git/etc/gitattributes 将是您的 system-wide gitattributes;请参阅我的回答中的 System-wide gitattributes 部分。至于~/.config/git,那是你的 global gitattributes;请参阅我的答案中的 Global (user-level) gitattributes 部分。
  • 因此(检查)任何etc 都将是系统范围的(或至少对于给定的Git 而言是系统范围的),而各种$XDG_CONFIG_HOME/git/...~/.configs/git/.. 无关紧要(对于@987654352 @ 或者)如果我将core.attributesfile(或core.excludesfile)设置为其他值。例如,如果我将其设置为 /.gitattributesviz. ~/.gitignore),这将用于我的“全局”(用户级)设置。
  • @raxacoricofallapatorius 如果我理解正确,答案是否定的。存储库级设置优先于用户级设置,用户级设置优先于系统范围设置。
  • 是的,回购级别取代了那些。我正在检查“全局”。
【解决方案2】:

如果您读到这里,仍然不知道$prefix 在哪里(即它不只是空白或/usr/local)或者为什么您的/etc/gitattributes 没有被读取,您可以使用strace 或类似工具查看 git 正在检查的所有地方。 Strace 在 stderr 上打印大量调试信息,因此请使用 2&gt;&amp;1 并过滤它,如下所示,使用 grep。我为示例选择的 git 命令使用属性文件,因为 --stat 会根据 -diff 属性进行更改,但是您当时尝试运行的任何 git 命令都应该没问题。

例如,使用 SCL 在 RHEL 上安装更新后的 git,您可能会得到以下结果:

$ strace -f git log --stat -1 2>&1 | grep --color 'open.*attr'
open("/opt/rh/rh-git218/root/usr/etc/gitattributes", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/home/username/.config/git/attributes", O_RDONLY) = -1 ENOENT (No such file or directory)
open(".gitattributes", O_RDONLY)        = -1 ENOENT (No such file or directory)
open(".git/info/attributes", O_RDONLY)  = -1 ENOENT (No such file or directory)

。 . .这表明这里的$前缀是/opt/rh/rh-git218/root/usr

Strace 的 -f 在这里不是必需的,但如果你不知道并且不在乎某些命令是否会分叉,那么首先添加 -f 可以消除你看不到你的东西的机会正在寻找。

【讨论】:

    猜你喜欢
    • 2016-05-23
    • 1970-01-01
    • 2010-12-01
    • 2018-09-28
    • 2019-10-04
    • 1970-01-01
    • 2020-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多