Git能永久忽略一个远程分支吗?

34
我正在使用GitHub的Pages功能。这个功能是通过将发布的HTML放在名为gh-pages的分支中来实现的。我有两个单独的工作目录,一个用于项目本身,另一个用于HTML文档。
对于前者,我想完全忽略gh-pages分支,因为它是一条不相关的工作线,并且我不希望它在我的各种提交可视化中混乱。
也就是说,我拥有的是:
$ git remote show origin
* remote origin
  Fetch URL: git@github.com:reidpr/quac.git
  Push  URL: git@github.com:reidpr/quac.git
  HEAD branch: master
  Remote branches:
    bar              tracked
    foo              tracked
    gh-pages         tracked
    master           tracked
  Local branches configured for 'git pull':
    master    merges with remote master
  Local refs configured for 'git push':
    master    pushes to master    (up to date)

我希望得到的是类似这样的东西:

$ git remote show origin
  [...]
  Remote branches:
    bar              tracked
    foo              tracked
    gh-pages         ignored
    master           tracked
  [...]

请注意,我希望追踪几个分支,只有一个我不想追踪。我想指定后者而不是前者。

我可以删除对 origin / gh-pages 的本地引用,但是下次我运行 git fetch 时它会再次出现。

4个回答

32

3
根据你的shell,你可能想引用命令为 git config --add remote.origin.fetch '^refs/heads/dependabot/*' - Yasushi Shoji
非常好用,谢谢。我刚刚在我的仓库.git/config[remote "origin"]部分添加了fetch = ^refs/heads/boring-branch - vladis

7
您可以修改 .gitconfig 文件,让 git 只获取您想要的内容:
   fetch = +refs/heads/mybranch:refs/remotes/origin/mybranch

此外,您可以为获取所需内容的 fetch 创建一个别名:
 git fetch origin +refs/heads/mybranch:refs/remotes/origin/mybranch

创建别名的方法很简单,只需在.gitconfig文件中添加新的别名即可:
 [alias]
   myfetch= git fetch origin +refs/heads/mybranch:refs/remotes/origin/mybranch

更新:

当然,您可以指定更多的分支:

 git fetch origin +refs/heads/master:refs/remotes/origin/master +refs/heads/develop:refs/remotes/origin/develop

11
有没有办法让我获取除了我不想要的内容以外的所有东西?在上面的例子中,我只想要一个远程分支,但实际上有好几个,只有一个我不想要。 - Reid
1
@Reid 很遗憾,我不知道如何直接排除一个特定的分支。但是我至少在答案中提供了更新,展示了如何指定要获取的更多分支。 - Ondrej Peterka
4
一种选择可能是使用一个脚本,挂钩到post-receive,以删除有问题的分支。不过,还没有完全考虑清楚这个答案。 - jb510

3

我刚遇到了同样的问题。我对“bob”的一个分支感兴趣,但他有许多分支使我的 git branch -a 输出混乱不堪。

我做了以下操作:

rm .git/refs/remotes/bob/{next,master,maint}

分支已经消失了。不过使用 git fetch 可能会恢复它们,但我并不打算经常从 Bob 那里获取。


1
这与 git branch -d -r bob/{next,master,maint} 相同。远程跟踪分支确实会在随后的 git fetch 后返回。 - Jivan Pal

0
如果您要将负的refspec添加到remote.origin.fetch中,请确保使用Git 2.38(2022年第三季度):"git remote show [-n] frotz"(man)现在会注意到负的pathspec。

请查看提交2c80a82(2022年6月16日),作者为Jacob Keller (jacob-keller)
(由Junio C Hamano -- gitster --于2022年7月13日合并至提交33f448b

远程:处理git remote show中的负refspecs

报告者:Pavel Rappo
签名者:Jacob Keller

By default, the [git remote show]https://github.com/git/git/blob/33f448b5fc916e6be266b857617a3b27a467da81/Documentation/git-remote.txt#L168-L187)(man) command will query data from remotes to show data about what might be done on a future git fetch.

This process currently does not handle negative refspecs.

This can be confusing, because the show command will list refs as if they would be fetched.

For example if the fetch refspec "^refs/heads/pr/*", it still displays the following:


 * remote jdk19
   Fetch URL: git@github.com:openjdk/jdk19.git
   Push  URL: git@github.com:openjdk/jdk19.git
   HEAD branch: master
   Remote branches:
     master tracked
     pr/1   new (next fetch will store in remotes/jdk19)
     pr/2   new (next fetch will store in remotes/jdk19)
     pr/3   new (next fetch will store in remotes/jdk19)
   Local ref configured for 'git push':
     master pushes to master (fast-forwardable)

Fix this by adding an additional check inside of get_ref_states.
If a ref matches one of the negative refspecs, mark it as skipped instead of marking it as new or tracked.

With this change, we now report remote branches that are skipped due to negative refspecs properly:

 * remote jdk19
   Fetch URL: git@github.com:openjdk/jdk19.git
   Push  URL: git@github.com:openjdk/jdk19.git
   HEAD branch: master
   Remote branches:
     master tracked
     pr/1   skipped           <=====
     pr/2   skipped           <=====
     pr/3   skipped           <=====
   Local ref configured for 'git push':
     master pushes to master (fast-forwardable)

By showing the refs as skipped, it helps clarify that these references won't actually be fetched.

This does not properly handle refs going stale due to a newly added negative refspec.
In addition, 'git remote' prune doesnt handle that negative refspec case either.
Fixing that requires digging into get_stale_heads and handling the case of a ref which exists on the remote but is omitted due to a negative refspec locally.


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接