Skip to content

Instantly share code, notes, and snippets.

@wolph
Created January 13, 2012 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolph/1604953 to your computer and use it in GitHub Desktop.
Save wolph/1604953 to your computer and use it in GitHub Desktop.
clean-merged-branches
#!/bin/sh
#/ Usage: clean-merged-branches [-f]
#/ Delete merged branches from the origin remote.
#/
#/ Options:
#/ -f Really delete the branches. Without this branches are shown
#/ but nothing is deleted.
#/ -n Dry-run, only show what would be removed.
set -e
# show usage maybe
[ "$1" = "--help" ] && {
grep '^#/' <"$0"| cut -c4-
exit 0
}
# fetch and prune remote branches
git fetch origin --prune
# grab list of merged branches
branches=$(
git branch -a --merged |
grep remotes/origin/ |
grep -v /master |
sed 's@remotes/origin/@@'
)
# bail out with no branches
[ -z "$branches" ] && {
echo "no merged branches detected" 1>&2
exit 0
}
# delete the branches or just show what would be done without -f
if [ "$1" = -f ]; then
git push origin $(echo "$branches" | sed 's/^ */:/')
elif [ "$1" = -n ]; then
echo "These branches will be deleted:" 1>&2
echo "$branches"
echo "Run \`$0 -f' if you're sure."
else
for branch in $branches; do
while true; do
echo
echo "Do you want to remove $branch? (Y/n)"
read REPLY
case $REPLY in
y | Y | '')
echo "Removing $branch"
git push origin $(echo "$branch" | sed 's/^ */:/')
break
;;
n | N)
echo "Skipping $branch"
break
;;
* )
echo "'$REPLY' is not a valid option, please try again"
;;
esac
done
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment