【问题标题】:How to ignore some branches/tags being copied to local git when do git fetch --allgit fetch --all 时如何忽略一些被复制到本地 git 的分支/标签
【发布时间】:2014-12-24 18:35:30
【问题描述】:

我的 git 中添加了许多遥控器。每个远程都是一个开发人员的存储库。每天我都会fetch --all 来查看他们创建的任何新分支并准备好进行审查。

但是,开发人员可以将“私有”分支推送到远程。比如说,所有带有下划线前缀的分支都没有准备好审查,而其他分支已经准备好审查。

在执行git fetch --all 时,我的 git 图表(/libexec/git-core/git-gui)将看到所有分支,无论它们是否具有下划线前缀。它使图表复杂化。

我希望 git fetch 忽略那些 _XXXX 分支被下载到我的本地 git。所以当我查看 git 图时,它就像:

  • 显示分支: RemoteA/Branch1 , RemoteB/Branch1, RemoteB/Branch2
  • 忽略分支: RemoteA/_Branch2, RemoteB/_Branch3

我该怎么做?

【问题讨论】:

  • 有关 Git 2.29(2020 年第四季度)引入的 negative refspec 的更多信息,请参阅“Git - What is “Refspec””。
  • 实际上git fetch --all 表示如果您有多个远程分支/标签,则从所有远程获取,而不是所有远程分支/标签。后者实际上由获取规范控制,因此在+refs/heads/* 中,* 表示从该远程获取所有磁头。所以你可以明确指定你想要的,或者在 Git 2.29+ 中使用负提取规范

标签: git git-branch git-fetch


【解决方案1】:

Git v2.29.0 更新

在 Git v2.29 中添加了negative refspecs,1,您可以在不改变同事习惯的情况下实现您的初衷。

对于每个同事的遥控器,添加一个否定的 refspec(即以 ^ 为前缀)以排除以 _ 为前缀的分支。这大致改编自the unit test for .git/configthe one for negative patterns。请参阅下面代码块中的最后一行以了解“Fred's”遥控器。

[remote "fred"]
    url = something.git
    fetch = +refs/heads/*:refs/remotes/fred/*
    fetch = ^refs/heads/_*

您有两个选项可以在命令行上为fred 设置此选项:

  • git config --add remote.fred.fetch '^refs/heads/_*'
  • git remote set-branches --add fred '^refs/heads/_*'

原答案

VonC's excellent answer 上扩展一个特殊的“review”文件夹,2您可以为同事的遥控器修改.git/config 的条目。这里是the relevant documentation

曾经的地方

[remote "fred"]
    url = something.git
    fetch = +refs/heads/*:refs/remotes/fred/*

你可以改成

[remote "fred"]
    url = something.git
    fetch = +refs/heads/review/*:refs/remotes/fred/review/*
    fetch = +refs/heads/other_pattern/*:refs/remotes/fred/other_pattern/*
    fetch = +refs/heads/specific_branch:refs/remotes/fred/specific_branch

完成此操作后,您可能需要清理对已获取的 ref 的引用,但未来的 git fetch --alls 或 git fetch freds 不会更新您指定的模式之外的任何内容。


1截至 2021 年 8 月 30 日,仍然没有关于负面 refspecs 的文档,因此最好的查看位置是 in Git's history
2或任意数量的特殊或有趣的文件夹/分支。

【讨论】:

    【解决方案2】:

    您可以使用ignore-refs 选项:

    git fetch --ignore-refs branch123
    

    【讨论】:

    • 我正在使用 git 2.8.4,在这篇文章之后发布,但我没有 ignore-refs 选项。我做了一些谷歌搜索,我对它的唯一引用are for git svn.
    【解决方案3】:

    您可以使用别名一步完成。将此添加到您的 ~/.gitconfig 文件中:

    [alias]
        fall = !sh -c 'git fetch --all && git branch -r | sed /HEAD/d | grep /_ | xargs git branch -dr' --
    

    然后说git fall。它将删除所有包含/_的远程分支。

    您可以观察到作为 shell 命令的别名的复杂性。 :-/

    【讨论】:

      【解决方案4】:

      您可以使用命名空间,而不是使用“_”命名约定,将分支推送到origin/review/Branch1 (git push Branch1:review/Branch1)
      (命名为“组”in that answer,或“that answer 中的分层分支名称(带有斜线的分支名称))

      这样,您只需获取“review”命名空间中的内容:

      git fetch +refs/heads/review/*:refs/remotes/origin/review/*
      

      唯一的其他选择是脚本,它会:

      【讨论】:

      • 但是如何为所有遥控器做呢? git fetch +refs/heads/review/*:refs/remotes/origin/review/* 意味着我必须在命令中输入 RemoteA 来替换“原点”。任何类似但适用于 --all 的东西?
      • @palazzotrain 您需要遍历所有遥控器(由git remote 列出):易于编写脚本。
      • @Michael 来自github.com/git/git/commit/…,参考规范代码在github.com/git/git/blob/…。在里面不要有任何shell扩展机制。
      • @Michael 回到 Git 1.5(2006 年 11 月),扩展已经通过代码完成:github.com/git/git/commit/…
      • @Michael 在stackoverflow.com/a/35995983/6309 中可以找到另一个对您答案的确认。
      猜你喜欢
      • 1970-01-01
      • 2016-10-08
      • 2016-08-06
      • 2018-05-20
      • 1970-01-01
      • 1970-01-01
      • 2011-07-30
      • 1970-01-01
      • 2013-07-01
      相关资源
      最近更新 更多