Git 为一个分支排除文件

标签 git gitignore git-config

我想忽略分支中的某些文件,而不必依赖跟踪的 .gitignore 文件,该文件将在与其他分支 merge 时被覆盖。

我密切关注了a Stack Overflow answer连同 the linked blog post ,但我的存储库似乎无法识别我的 .git/config 中指定的 excludesfile。 Git 文档(git-configgitignore)似乎没有说明如何指定和处理特定分支的排除文件。

我的 .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/

相关文章:

每次我推送时,Git 都会询问用户名

git - 我仍然可以检查已删除分支中的任何提交,它是永久性的吗?

git - 如何修改git add来处理删除的文件?

git - 将 TeamCity 与 BitBucket 结合使用

git - 将git config文件设置推送到远程URL

csv - 添加作为带空格的逗号分隔值的环境变量时出现 Heroku 错误

javascript - 是否应该将 package-lock.json 文件添加到 .gitignore 中?

Gitignore 不会更新我的仓库中已有的文件

Git 编辑器错误

windows - 将一堆文件从 Windows 复制到 ubuntu 服务器,现在所有文件都显示在 git 中已修改,并带有 ^M