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.
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.
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 .
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/.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With