Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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.

like image 625
user716468 Avatar asked Mar 27 '13 00:03

user716468


People also ask

How do I delete local branches no longer on my remote?

Remove All Local Branches not on Remote First we get all remote branches using the git branch -r command. Next, we get the local branches not on the remote using the egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) command, Finally we delete the branches using the xargs git branch -d command.

How do I force delete a remote branch?

Deleting remote branches To delete a remote branch, you can't use the git branch command. Instead, use the git push command with --delete flag, followed by the name of the branch you want to delete. You also need to specify the remote name ( origin in this case) after git push .

How do I stop a remote branch from tracking?

Simply delete your remote tracking branch: git branch -d -r origin/<remote branch name> (This will not delete the branch on the remote repo!) That will make any push/pull completely unaware of origin/.

How do I clean up remote branches?

In order to clean up remote tracking branches, meaning deleting references to non-existing remote branches, use the “git remote prune” command and specify the remote name. In order to find the name of your current configured remotes, run the “git remote” command with the “-v” option.


1 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.

like image 129
user716468 Avatar answered Sep 22 '22 10:09

user716468