Skip to content

Instantly share code, notes, and snippets.

@brysonreece
Last active November 3, 2023 04:44
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 brysonreece/cc4c7fa99fcadb633ed5853d4c90dcf9 to your computer and use it in GitHub Desktop.
Save brysonreece/cc4c7fa99fcadb633ed5853d4c90dcf9 to your computer and use it in GitHub Desktop.
Remove Untracked Git Branches
#! /bin/zsh
#
# @author Bryson Reece <hey@bryson.cc>
# @license none
#
CLEAR_ALL="\033[0m"
CLEAR_UL="\033[24m"
CLEAR_LNE="\033[0K"
FAINT="\033[2m"
UNDERLINE="\033[4m"
YELLOW="\033[33m"
YELLOW_BOLD="\033[1;33m"
GREEN_BOLD="\033[1;32m"
RED_BOLD="\033[1;31m"
# loop over all branches that are no longer on remote (except for main/staging) and ask if they should be deleted
for BRANCH in $(git fetch -p && git branch -vv | awk '/: gone]/{print $1}' | grep -x -v 'main\|staging'); do
echo -en "\t\tPermanently delete the following untracked branch? "
echo -en "${YELLOW}${UNDERLINE}${BRANCH}${CLEAR_UL}${CLEAR_ALL} "
echo -en "[y/n] "
read -srq shouldDelete
if [[ $shouldDelete == "y" ]]
then
echo -en "\r${CLEAR_LNE}${YELLOW_BOLD}[DELETING]${CLEAR_ALL}\t${BRANCH} "
git branch -D $BRANCH > /dev/null
echo -e "\r${CLEAR_LNE}${RED_BOLD}${FAINT}[DELETED]${CLEAR_ALL}\t${FAINT}${BRANCH}${CLEAR_ALL} "
else
echo -e "\r${CLEAR_LNE}${GREEN_BOLD}${FAINT}[SKIPPED]${CLEAR_ALL}\t${FAINT}${BRANCH}${CLEAR_ALL} "
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment