Skip to content

Instantly share code, notes, and snippets.

@wdawson
Created January 4, 2017 19:57
Show Gist options
  • Save wdawson/39a3d018add4b188efb48e12bd7a76db to your computer and use it in GitHub Desktop.
Save wdawson/39a3d018add4b188efb48e12bd7a76db to your computer and use it in GitHub Desktop.
Bash script to remove git branches and remote tracking branches (named the same thing in your origin remote)
#!/bin/bash
RED='\033[1;31m'
YELLOW='\033[1;33m'
WHITE='\033[1;37m'
GREEN='\033[1;32m'
NO_COLOR='\033[0m'
if [ ! -d ".git" ] || !(git rev-parse --git-dir > /dev/null 2>&1); then
echo -e "${YELLOW}No git repository found here${NO_COLOR}"
exit 1
fi
BRANCHES=$(git for-each-ref --format '%(refname)' refs/heads/ | cut -d/ -f3)
for CURRENT_BRANCH in $BRANCHES; do
if [ "$CURRENT_BRANCH" = "master" ]; then
echo -e "Skipping master branch..."
continue
fi
echo -e "Do you want to remove ${WHITE}$CURRENT_BRANCH${NO_COLOR} ?[Y/n/exit]"
read CHOICE
case $CHOICE in
y|Y|'')
echo -e "${RED}Deleting branch...${NO_COLOR}"
git push origin :$CURRENT_BRANCH
git branch -D $CURRENT_BRANCH
;;
n|N)
echo -e "Skipping..."
;;
'exit')
break
;;
*)
echo -e "${YELLOW}Invalid Input.${NO_COLOR} Skipping..."
echo -e "Usage: 'y'(default), 'n', or 'exit'"
;;
esac
done
echo -e "${GREEN}Done${NO_COLOR}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment