Skip to content

Instantly share code, notes, and snippets.

@yarolegovich
Created April 29, 2022 07:21
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 yarolegovich/9c59d40a89ea0d30a3b377eea2cbabd9 to your computer and use it in GitHub Desktop.
Save yarolegovich/9c59d40a89ea0d30a3b377eea2cbabd9 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Go through all the branches except the current active, asking whether it should
# be preserved or removed. Delete all marked for removal in the second iteration.
branches=()
while IFS= read -rs line ; do
branches+=("${line:2}")
done < <(git branch)
i=0
toDelete=()
tput sc
while [ $i -lt ${#branches[@]} ] ; do
tput rc; tput el
if [ $i -gt 0 ] ; then
choices="(y[es]/n[o]/e[xit]/b[ack])"
else
choices="(y[es]/n[o]/e[xit])"
fi
branch=${branches[$i]}
printf "Delete %s %s? " "$branch" "$choices"
while read -r -n1 ans ; do
if [[ $ans == 'y' || $ans == 'n' || $ans == 'e' || $ans == 'b' && $i -gt 0 ]] ; then
break
fi
done
if [ "$ans" == 'e' ] ; then
echo ""
exit 1
fi
if [ "$ans" == 'y' ] ; then
toDelete+=("$branch")
step=1
elif [ "$ans" == 'b' ] ; then
toDelete=("${toDelete[@]/$branch}")
step=-1
else
toDelete=("${toDelete[@]/$branch}")
step=1
fi
((i+=step))
done
tput rc; tput el
for branch in "${toDelete[@]}"; do
git branch -D "$branch"
done
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment