Skip to content

Instantly share code, notes, and snippets.

@anaisbetts
Created March 15, 2014 21:01
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anaisbetts/9573852 to your computer and use it in GitHub Desktop.
Save anaisbetts/9573852 to your computer and use it in GitHub Desktop.
#!/bin/bash
git branch --merged | while read line
do
# If the line is in the format '* master' (indicating the current branch),
# this will be effectively empty, so we don't somehow delete the current
# branch
BRANCH=`echo "$line" | awk -F '*' '{ print $1 }'`
if [ -z "$BRANCH" ]
then
continue
fi
if [ "$BRANCH" == "master" ]
then
continue
fi
git branch -d "$BRANCH"
done
#!/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.
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 origin/master |
grep remotes/origin/ |
grep -v /master |
grep -v 'enterprise-.*-release' |
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/^ */:/')
else
echo "These branches will be deleted:" 1>&2
echo "$branches"
echo "Run \`$0 -f' if you're sure."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment