Last Updated: June 18, 2016
·
14.43K
· wilhelmbot

Remove a thousand stale remote branches on git

If you find yourself working on a git repository flooded by outdated or merged branches, check this:

1 - List all your remote branches:

$ git branch -r

2 - Filter the branches by some regular expression. In this case I'm interested in deleting any branch with the 'feature-' prefix:

$ git branch -r | awk -F/ '/\/feature-/{print $2}'

3 - Pipe the last command to git push to delete them:

$ git branch -r | awk -F/ '/\/feature-/{print $2}' | xargs -I {} git push origin :{}

4 - Grab a beer.

5 - Remove any local reference to those branches:

$ git remote prune origin

References:

http://stackoverflow.com/questions/10555136/delete-multiple-remote-branches-in-git

http://stackoverflow.com/questions/1072171/how-do-you-remove-an-invalid-remote-branch-reference-from-git

2 Responses
Add your response

As covered here:

http://stackoverflow.com/questions/18308535/automatic-prune-with-git-fetch-or-pull

You can now auto prune as of git v1.8.5 with:

git config remote.origin.prune true
over 1 year ago ·

We use git-obsolete-branch-remover useful as it lists or removes local or remote Git branches based on last commit date and merge status.
https://github.com/vackosar/git-obsolete-branch-remover

over 1 year ago ·