git可以永久忽略远程分支吗?

时间:2013-05-30 17:28:42

标签: git branch

我正在使用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时会再次显示。

3 个答案:

答案 0 :(得分:7)

您可以修改.gitconfig,因此它会告诉git仅提取您想要的内容:

   fetch = +refs/heads/mybranch:refs/remotes/origin/mybranch

此外,您可以为获取所需内容的提取创建别名:

 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

答案 1 :(得分:5)

october 2020发布git 2.29起,您就可以利用否定refspec排除要提取的特定分支。

对于GitHub Pages,您可以这样做:

git config --add remote.origin.fetch ^refs/heads/gh-pages

和Dependabot:

git config --add remote.origin.fetch ^refs/heads/dependabot/*

您可以在https://github.blog/2020-10-19-git-2-29-released/#user-content-negative-refspecs

上了解更多信息

答案 2 :(得分:4)

我刚刚遇到同样的问题。我对“bob'”中的一个分支很感兴趣,但他有很多分支使我的git branch -a输出变得混乱。

我这样做了:

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

然后树枝消失了。 git fetch可能会恢复它们,但我不打算定期从bob中获取。