Git排除了分支的文件

gor*_*rds 19 git gitignore git-config

我想忽略分支中的某些文件,而不必依赖在.gitignore与其他分支合并期间将被覆盖的跟踪文件.

我一直密切关注Stack Overflow的回答以及链接的博客帖子,但我的回购似乎并没有认识到excludesfile我的指定.git/config.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
Run Code Online (Sandbox Code Playgroud)

我的.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
Run Code Online (Sandbox Code Playgroud)

.gitignore我的"specialbranch"中没有文件.

如果我试图忽略像这样的文件info.php,我的.git/info/exclude_specialbranch文件看起来像这样:

info.php
Run Code Online (Sandbox Code Playgroud)

...但它不会忽略该文件.

如果我运行git status --ignored,它只列出.DS_Store默认exclude文件中的文件.

但是,如果我添加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
Run Code Online (Sandbox Code Playgroud)

它完全忽略了文件.

请注意,没有线仍然工作excludesfile = +/info/exclude[core].git/config.Git似乎知道系统范围在哪里,exclude我不必告诉它.

我有一种感觉,.git/config即无法识别我的自定义排除文件的地址:

excludesfile = +/info/exclude_specialbranch
Run Code Online (Sandbox Code Playgroud)

...很可能是因为使用了+描述.git目录的位置.

在(最近的)Git版本中解决自定义排除文件的正确方法是什么?

我正在运行OSX 10.9.5和Git 2.2.1版.

Pal*_*lec 28

Git不支持每个分支排除文件

你正在努力实现Git不支持的东西.该博客文章是这个骗局是原始出处堆栈溢出的答案只能人云亦云.正如在该答案的评论中所指出的那样,即使是最初的博客文章也包含了一些讨论,该讨论表明该解决方案不起作用,并且它链接到更新的博客帖子,该帖子提到甚至作者也无法重现该行为.

为什么它不起作用?如果你读man gitignoreman git-config,你会发现只是core.excludesfile引用.没有branch.<name>.excludesfile.这core.excludesfile意味着您可以排除例如Vim .swp文件或您的软件使用的其他临时内容.

core.excludesfile

除了.gitignore(每个目录)之外.git/info/exclude,Git 还会查看此文件中的文件模式,这些文件不是要跟踪的." ~/"扩展为指定用户主目录的值$HOME和" ~user/".它的默认值是$XDG_CONFIG_HOME/git/ignore.如果$XDG_CONFIG_HOME未设置或为空,$HOME/.config/git/ignore则使用.见gitignore(5).

变通

我相信每个分支排除文件的最佳近似值是使用post-checkout钩子实现的,并.gitignore通过符号链接实现.

每个分支将具有例如.gitignores具有以相应分支命名的文件的目录.然后会有一个.gitignores/__default默认使用的文件.该.gitignore会由所有不包括文件被排除在外,并将由结账后钩的符号链接到相应的文件被创建.gitignores.

如果您不想跟踪排除文件,则可以使用.git/info/exclude文件作为符号链接.git/info/excludes/__default等来执行相同操作.

  • 实际上,我还没有100%确定它从未奏效.我确信它从未被记录过.令你感到困惑的是你试图遵循的答案的大量赞成.我赞成它,赞成评论说它不起作用,我建议任何读这篇文章的人都这样做. (2认同)
  • Hook 是单独的可执行文件或脚本,Git 不会创建它们。我可能永远不会编写这样的脚本,因为我不需要该功能,而且它并不简单。如果您在自己实施时遇到任何问题并且找不到答案,您可以发布有关它的新问题。@tukusejssirs (2认同)