Skip to content

Instantly share code, notes, and snippets.

@epleterte
Created April 6, 2017 08:35
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 epleterte/03ce892996faadd72aff2006506e2ad0 to your computer and use it in GitHub Desktop.
Save epleterte/03ce892996faadd72aff2006506e2ad0 to your computer and use it in GitHub Desktop.
#!/bin/bash -ue
# Christian "Superstar" Bryn <chr.bryn@gmail.com> 1850-2017
function print_usage {
cat <<EOF
Usage: ${0} [-h|-a|-b|-q] <branch1> [<branch2>]
-h Print usage
-a All - automatically check all branches and delete if merged.
-b Batch - skip 'destructive action' warning.
-q Quiet - turn off verbosity.
Examples:
${0} my-feature-branch
${0} -a
EOF
}
all="false"
batch="false"
while getopts abhq o
do
case $o in
h)
print_usage ; exit ;;
a)
all="true" ;;
b)
batch="true" ;;
q)
verbose="false" ;;
esac
done
shift $(($OPTIND-1))
if [[ "${all}" == "true" ]]
then
if [[ "${batch}" == "false" ]]
then
echo "This cleanup can be crude - be sure to know what you're doing"
echo "Press ENTER to continue, or CTRL+C to abort."
read
fi
true
branches=$(git branch -r --merged origin/master | grep origin | grep -v '>' | grep -v master | xargs -L1 | cut -d"/" -f2- )
else
[[ $# -eq 0 ]] && { echo ">> No branches specified"; print_usage; exit 1; }
branches="$@"
fi
for branch in ${branches}; do
git branch | grep -q ${branch} || { echo ">> '${branch}' does not seem to be a valid (local) branch, skipping"; continue; }
[[ "${all}" == "true" ]] && echo ">>> found merged branch ${branch}"
echo ">> deleting local branch first..." && \
git branch -d "${branch}" && \
echo ">> deleting remote branch" && \
git push origin --delete "${branch}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment