我想忽略分支中的某些文件,而不必依赖跟踪的 .gitignore
文件,该文件将在与其他分支 merge 时被覆盖。
我密切关注了a Stack Overflow answer连同 the linked blog post ,但我的存储库似乎无法识别我的 .git/config
中指定的 excludesfile
。 Git 文档(git-config
、gitignore
)似乎没有说明如何指定和处理特定分支的排除文件。
我的 .git/config
看起来像这样:
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
excludesfile = +/info/exclude
[branch "myspecialbranch"]
excludesfile = +/info/exclude_specialbranch
我的 .git/info/exclude
文件看起来像这样(默认情况下,我没有碰它):
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
.DS_Store
我的“specialbranch”中没有.gitignore
文件。
如果我尝试忽略像 info.php
这样的文件,我的 .git/info/exclude_specialbranch
文件如下所示:
info.php
... 但它不会忽略该文件。
如果我运行 git status --ignored
,它只会列出默认 exclude
文件中的 .DS_Store
文件。
但是,如果我将 info.php
添加到我的 .git/info/exclude
文件中:
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
.DS_Store
info.php
它完全忽略了该文件。
请注意,它在 .git/config
中的 [core]
下没有行 excludesfile = +/info/exclude
仍然可以工作。 Git 似乎无需我告诉它就知道系统范围的 exclude
在哪里。
我感觉 .git/config
无法识别我的自定义排除文件的地址:
excludesfile = +/info/exclude_specialbranch
...很可能是因为使用+
来描述.git
目录的位置。
在(较新的)Git 版本中处理自定义排除文件的正确方法是什么?
我正在运行 OSX 10.9.5 和 Git 版本 2.2.1。
最佳答案
Git 不支持每个分支排除文件
你正在尝试实现 Git 不支持的东西。 blog post是这个骗局的原始来源the Stack Overflow answer只会鹦鹉学舌。正如该答案下的评论所述,即使是原始博客文章也包含讨论,指出该解决方案不起作用并且它链接到 newer blog post。这提到即使是作者也无法重现该行为。
为什么它不起作用?如果你阅读 man gitignore
和 man git-config
, 你会发现 core.excludesfile
引用。没有 branch.<name>.excludesfile
那里。 core.excludesfile
旨在使您能够排除例如Vim .swp 文件或您的软件使用的其他临时文件。
core.excludesfile
In addition to
.gitignore
(per-directory) and.git/info/exclude
, Git looks into this file for patterns of files which are not meant to be tracked. "~/
" is expanded to the value of$HOME
and "~user/
" to the specified user’s home directory. Its default value is$XDG_CONFIG_HOME/git/ignore
. If$XDG_CONFIG_HOME
is either not set or empty,$HOME/.config/git/ignore
is used instead. See gitignore(5).
解决方法
我相信每个分支排除文件的最佳近似是使用 post-checkout hook 实现的和实现 .gitignore
通过符号链接(symbolic link)。
每个分支都会有例如一个.gitignores
包含以相应分支命名的文件的目录。然后会有一个.gitignores/__default
默认情况下使用的文件。 .gitignore
将被所有排除文件排除,并将由 checkout 后 Hook 创建为指向 .gitignores
中相应文件的符号链接(symbolic link)。 .
如果您不想跟踪排除文件,您可以对 .git/info/exclude
执行相同的操作文件作为 .git/info/excludes/__default
的符号链接(symbolic link)等
关于Git 为一个分支排除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29579546/