Skip to content

Instantly share code, notes, and snippets.

@lorenmh
Created May 25, 2016 18:14
Show Gist options
  • Save lorenmh/85745aadad75b1e10bdb388026618edd to your computer and use it in GitHub Desktop.
Save lorenmh/85745aadad75b1e10bdb388026618edd to your computer and use it in GitHub Desktop.
clean branches from current git directory
#!/bin/bash
# Loren - 2/12/16
# you can provide some grep regex as the first arg if you want to delete only
# specific branches
if [ $1 ]
then
branches="git branch | sed 's/^\*//' | grep $1"
else
branches="git branch | sed 's/^\*//'"
fi
for branch in $(eval $branches)
do
have_user_input=0
should_delete=0
while ! (( $have_user_input ))
do
echo Delete branch $branch? [ynq]
read answer
if [ "$answer" == "y" ] || [ "$answer" == "yes" ]
then
should_delete=1
have_user_input=1
elif [ "$answer" == "n" ] || [ "$answer" == "no" ]
then
should_delete=0
have_user_input=1
elif [ "$answer" == "q" ] || [ "$answer" == "quit" ] || [ "$answer" == "exit" ]
then
echo Exiting
exit
else
echo 'Invalid input, enter y or n or q'
fi
done
if [ $should_delete -eq 1 ]
then
git branch -D $branch
else
echo Not deleting branch $branch.
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment