Last Updated: February 25, 2016
·
471
· danielmiladinov

Prune Dead Branches From Your Remotes

This is great if you've got remotes that are downstream or you treat as backups.

Pushing new branches to a remote is pretty simple, but deleting remote branches isn't as easy.

Place this in your ~/.bashrc:

function prune-remote () {
    for branch in `git remote show ${1} | grep tracked | awk '{print $1}'`;
    do 
        isLocal=`git branch | grep ${branch}`; 
        if [ -z "$isLocal" ]; then 
            git push ${1} :${branch}; 
        fi; 
    done
}

And then in your ~/.gitconfig, you can define a new alias, prune-remote or whatever you want to call it:

[alias]
    prune-remote = !bash -ic 'prune-remote $@' -

To use it, it's very simple. Suppose you have a remote named backup.
All you'd need to run then is: git prune-remote backup.

Check out my gist for it as well.

1 Response
Add your response

If you want to only remove remotes that have been merged into master, you can use git-sweep or git-branch-delete-orphans.

over 1 year ago ·