I have a git repo with tens of remotes which have been merged into master. I can delete these remotes one at a time by using:
git push --delete origin myBranch-1234
However this is a slow and tedious process for all remotes. So I'm trying this command:
git branch -r --merged | grep origin | grep -v master | xargs git push origin --delete
git branch -r --merged
lists all merged remotes.grep origin
tells the command to include origin.grep -v master
tells the command to exclude master.xargs git push origin --delete
tells the command to delete the list of remotes.
All together, I expect this to gather all merged remotes and delete them.
When I run the above command, I receive the following for every merged remote;
error: unable to delete 'origin/myBranch-1234': remote ref does not exist error: unable to delete 'origin/myBranch-1235': remote ref does not exist error: unable to delete 'origin/myBranch-1236': remote ref does not exist error: unable to delete 'origin/myBranch-1237': remote ref does not exist ... etc
However these remotes do exist and I can checkout each of them. Many sites and people recommend that I run git fetch --prune
to clean up missing references. This does nothing because all of these remotes exist.
I think I'm missing something small. Every time I research this, it seems like I'm doing this correctly, but I'm getting the above errors.
Use the -D switch to delete it irrespective of its merged status. to delete the ref.
You can use the prune subcommand of git-remote for cleaning obsolete remote-tracking branches. Alternatively, you can use the get-fetch command with the --prune option to remove any remote-tracking references that no longer exist on the remote. That's all about deleting remote-tracking branches in Git.
You may need to prune your local "cache" of remote branches first. Try running:
git fetch -p origin
before deleting.
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