Skip to content

Instantly share code, notes, and snippets.

@uasi
Created October 24, 2014 05:19
Show Gist options
  • Save uasi/8d8966f18dcd4504f428 to your computer and use it in GitHub Desktop.
Save uasi/8d8966f18dcd4504f428 to your computer and use it in GitHub Desktop.
git prune-branches
#!/bin/sh
#
# git-prune-branches - prune merged branches
#
# Configurations:
# .git/protected-branches - one-line regex matching the names of the branches to be protected.
#
if [ "`git branch | grep '^\*'`" != "* master" ]; then
echo "Error: not on master."
exit 1
fi
protected_branches=$( cat "`git rev-parse --git-dir`/protected-branches" 2> /dev/null | head -n1 )
protected_branches="${protected_branches}${protected_branches:+|}^master$"
echo "Deleting branches ignoring $protected_branches"
git branch --merged | cut -c3- | egrep -v "$protected_branches" | xargs git branch -d
git remote prune origin
@uasi
Copy link
Author

uasi commented Oct 24, 2014

Usage:

$ echo '^dont-delete$|^important-branches/' > .git/protected-branches
$ git checkout master
$ git prune-branches
Deleting branches ignoring ^dont-delete$|^important-branches/|^master$
Deleted branch foo (was fa8a3dd).
Pruning origin
URL: git@github.com:my/repo.git
 * [pruned] origin/foo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment