user716468
user716468

Reputation: 1623

How to remove all remote tracking branches that still exist on remote but no longer in my fetch refspec

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.

Upvotes: 13

Views: 4628

Answers (2)

user716468
user716468

Reputation: 1623

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.

Upvotes: 19

robrich
robrich

Reputation: 13205

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.

Upvotes: 1

Related Questions