Skip to content

Instantly share code, notes, and snippets.

@jnaO
Created July 4, 2019 16:45
Show Gist options
  • Save jnaO/776f2638b2310843e6ba600a6b9a0ee4 to your computer and use it in GitHub Desktop.
Save jnaO/776f2638b2310843e6ba600a6b9a0ee4 to your computer and use it in GitHub Desktop.
git delete local branches
#!/bin/bash
# Author:
# jnaO
red=$(tput setaf 1)
yellow=$(tput setaf 3)
blue=$(tput setaf 4)
green=$(tput setaf 7)
normal=$(tput sgr0)
for branch in $(git for-each-ref --format='%(if)%(HEAD)%(then)*%(end)%(refname:short)' refs/heads/); do
if [[ $branch == 'master' || $branch == *"*"* ]] ; then
continue
fi
read -p "${yellow}Delete $branch?${normal} (${purple}Y${normal}/n) " DELETE_BRANCH
if [[ $DELETE_BRANCH != 'n' ]] ; then
printf "${red}"
git branch -D $branch
printf "${normal}"
else
printf "${blue}Kept branch $branch${normal}\n"
fi
done
printf "\n\n${yellow}Remaining branches:${normal}\n"
for branch in $(git for-each-ref --format='%(if)%(HEAD)%(then)*%(end)%(refname:short)' refs/heads/); do
if [[ $branch == *"*"* ]] ; then
printf "${blue}${branch}${normal}"
else
printf " ${branch}"
fi
printf "\n"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment