GIT: remove remote branches with no local tracking branch

ⅰ亾dé卋堺 提交于 2019-12-10 17:17:33

问题


Let's say that git branch -a outputs something like this:

  • A
  • B
  • C
  • *master
  • remotes/origin/A
  • remotes/origin/B
  • remotes/origin/C
  • remotes/origin/master
  • remotes/origin/X
  • remotes/origin/Y
  • remotes/origin/Z

In bold are those branches (X, Y, Z) that are in the remote repository but I have not checked them out yet and I don't even intend to, they are related to a project I don't participate in.

Is there a command to remove all remote branches (those that are saved on MY repository, not the actual remote branch in the remote) which do not have a local tracking branch?

Edit: I intend to remove remote branches that are saved in my repository, showing the existence of the branch in the remote. I do not want to remove the actual branches in the remote.

Edit 2: Clarification in comment to mattmilten

You can recreate all remote branches with git pull, but I don't always git pull.

I often just git fetch origin master A B C and then merge or rebase or do anything I want with branches master, A, B or C.

Point is, whenever I "git pull" (for some reason), all the branches I don't want are created and it seems I have no simple option to remove them afterwards.


回答1:


No, this is not possible. The information about (all) branches is a part of your repo and will always be copied when a new clone is created. You need to ignore these branches or simply run git branch to list only your local branches.

How would you be able to retrieve remote branches at a later time, if you delete the information about their existence?




回答2:


I had this same problem but in a different situation. I deleted a local branch experiment1. I forgot to git branch --unset-upstream first, so I still had a remote tracking branch. Branch experiment1 was deleted from the remote after.

Later, when I ran git branch -a, it keep showing up in output as remotes/origin/experiment1. Neither git branch --unset-upstream nor git branch --delete would get rid of it because the local branch was already gone. Referencing it as origin/experiment1 instead of experiment1 did not work either.

So this is what I did:

  1. cd .git/refs/remotes/origin
  2. rm experiment1

And it no longer shows up in output and normal Git sync commands worked as expected after.

I am adding this answer because this is the top hit in an Internet search for "git remove remote-tracking branches with no local branch". Also, this question is different enough from "Remove tracking branches no longer on remote" question to not be a duplicate. If I were to wrote my own question that this answer answers, then that question would be a duplicate of this question.



来源:https://stackoverflow.com/questions/23266892/git-remove-remote-branches-with-no-local-tracking-branch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!