Created
January 9, 2017 15:15
-
-
Save kevsmith/27c3d64b9e17340d665d39fa600e1393 to your computer and use it in GitHub Desktop.
Keep local branches in sync with merged branches
This file contains 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 "-d, --delete Delete all dead branches" | |
echo "-e, --regex Change the regex used to select branches" | |
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 | |
git fetch --all --prune | |
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