Last active
July 18, 2016 17:44
-
-
Save kevsmith/e259ecbf6cbcd2cd1c46860d93eceaaf to your computer and use it in GitHub Desktop.
git helper script for managing local branch proliferation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
set -e | |
delete_branch() { | |
git branch -d "$1" | |
} | |
usage() { | |
echo "git purge [-d|--delete] [-e|--regex <regular_expression>]" | |
echo "" | |
echo "Deletes local branches with remotes which have been merged to master." | |
echo "" | |
echo "--delete Delete all dead branches" | |
echo "--regex Change the regex used to select branched." | |
echo "" | |
echo "NOTE: git purge will not delete the master branch." | |
} | |
kept_branches="v0\.*" | |
do_delete=0 | |
while [ "$1" != "" ]; | |
do | |
case $1 in | |
"-d") | |
do_delete=1 | |
;; | |
"--delete") | |
do_delete=1 | |
;; | |
"-e") | |
kept_branches="$2" | |
shift | |
;; | |
"--regex") | |
kept_branches="$2" | |
shift | |
;; | |
"-h") | |
usage | |
exit 0 | |
;; | |
"--help") | |
usage | |
exit 0 | |
;; | |
*) | |
usage | |
exit 1 | |
esac | |
shift | |
done | |
branches=`git branch --merged | grep -v -E "master|^\*|$kept_branches" | sort` | |
for branch in $branches | |
do | |
if [ "$do_delete" == "1" ]; then | |
delete_branch $branch | |
else | |
echo "Dead: $branch" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment