Skip to content

Instantly share code, notes, and snippets.

@janv
Last active October 6, 2019 09:54
Show Gist options
  • Save janv/8b4f41e7222a52394f247a90513435aa to your computer and use it in GitHub Desktop.
Save janv/8b4f41e7222a52394f247a90513435aa to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -euo pipefail
file="/tmp/git-cleanup-branches-$(uuidgen)"
function removeCurrentBranch {
sed -E '/\*/d'
}
function leftTrim {
sed -E 's/\*?[[:space:]]+//'
}
all_branches=$(git branch -vv | removeCurrentBranch | leftTrim)
# write branches to file
IFS=$'\n'
for branch in $all_branches; do
if echo $branch | grep -q ": gone"; then
echo "del $branch" >> $file
fi
done
for branch in $all_branches; do
if echo $branch | grep -vq ": gone"; then
echo "keep $branch" >> $file
fi
done
unset IFS
# write instructions to file
echo "
# All of your branches are listed above
# (except for the current branch, which you can't delete)
# change keep to d to delete the branch
# all other lines are ignored" >> $file
# prompt user to edit file
$EDITOR "$file"
DELETE_LIST=''
# check each line of the file
while read -r line; do
# if the line starts with "d "
if echo $line | grep --extended-regexp "^d[a-z]* " > /dev/null; then
# delete the branch
branch=$(echo $line | awk '{print $2}')
DELETE_LIST="$DELETE_LIST $branch"
# echo git branch -D $branch
fi
done < $file
git branch -D $DELETE_LIST
# clean up
rm $file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment