I recently reorganized my refspec of remote.origin.fetch
and now only fetch a small subset of branches from remote. However, git branch -a
shows me a lot of remote branches that I fetched previously, although they are no longer fetched now. Using git prune
does NOT help because those remote tracking branches do exist in remote.
2 Answers
The answer from robrich has a good hint: You can just remove every remote-tracking branch (or even the remote), and then use git fetch
to grab only those you want now from scratch.
If you do try to remove the remote all together, you may want to backup your .git/config file, so that when you add the remote back later, you can pick up the per-remote setting from the backup.
However, removing remote does not remove the remote-tracking branches for me. Maybe my local repo is bad. For any one who has the same problem, what I ended up doing is:
# This deletes all remote tracking branches for all remotes. So be careful if you have multiple remotes.
git branch -r | xargs -L 1 git branch -rD
Also, I have a lot of tags from the remote, which slow things down. I did this too:
# Be careful! This deletes EVERY tag!
git tag | xargs -L 1 git tag -d
You may want to configure git fetch
to not fetch all those tags back next time, which is beyond the scope of this question.
You can delete the remote and re-add it, then re-configure the remote.origin.fetch
. It's hitting the ant hill with a mallet, but it'll get the job done. You'll still need to delete the local branches (if any), but that's a mere git branch -D theOffendingBranchName
.
Edit: If you're feeling adventurous, you could go pillage through .git/refs/
deleting files you don't like. Make a backup of your .git folder first though -- in case the pruning goes very wrong.
-
Thanks for your answer. It turns out that
git remote rm
fails to remove remote-tracking branches, even aftergit gc
. I ended up doinggit branch -D
for every remote-tracking branch. Mar 28, 2013 at 0:51 -
Also, deleting .git/refs/remotes/origin does not help either. I suspect that many remote tracking branches are already packed and therefore do not appear as files there. Mar 28, 2013 at 0:52
-
Good idea. You could pillage through
.git/packed-refs
, though if deleting the remote worked, problem solved.– robrichMar 28, 2013 at 19:14 -
This doesn't work for me using git 2.8.1. It seems to be looking only for local branches
error: branch 'origin/master' not found.
To specify the remote tracking branch, I have to passr
to it withgit branch -rD origin/master
Jun 29, 2016 at 16:21