Skip to content

Instantly share code, notes, and snippets.

@inigomarquinez
Created September 13, 2022 16:42
Show Gist options
  • Save inigomarquinez/42f29dd8b2cd2e8a5042d9ac2dbc984f to your computer and use it in GitHub Desktop.
Save inigomarquinez/42f29dd8b2cd2e8a5042d9ac2dbc984f to your computer and use it in GitHub Desktop.
Remove all local branches
#! /bin/bash
help(){
echo -e 'Usage: dracarys-git-local-branches <main-branch>\n'
echo -e '\tRemove all local branches except main branch.'
echo -e '\tMain branch should be master or main, depending on your project'
exit 1
}
MAIN_BRANCH="$1"
if [ -z $MAIN_BRANCH ]; then
echo -e "Main branch name not found!"
help
fi
echo -e "Are you sure you want to remove all your local git branches? (Press CTRL+C to cancel)"
echo -n "Only 'yes' will be accepted to continue: "
read NEW_VALUE
if [ "$NEW_VALUE" = "yes" ]; then
git for-each-ref --format '%(refname:short)' refs/heads | grep -v $MAIN_BRANCH | xargs git branch -D
else
echo -e "Canceled!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment