Skip to content

Instantly share code, notes, and snippets.

@scruti
Created November 9, 2018 10:58
Show Gist options
  • Save scruti/f9938e0f9c3277c8e62f292512c2dcc5 to your computer and use it in GitHub Desktop.
Save scruti/f9938e0f9c3277c8e62f292512c2dcc5 to your computer and use it in GitHub Desktop.
ZSH function to delete git local branches
git-clean-branches() {
if [ ! -d .git ]; then
echo "This is not a Git directory"
return 1
fi;
# Get list of local branches
branches=()
eval "$(git for-each-ref --shell --format='branches+=(%(refname))' refs/heads/)"
current_branch="$(git rev-parse --abbrev-ref HEAD)"
for branch in "${branches[@]}"; do
# Strip the "ref/heads" prefix from the branch name
branch=${branch#refs/heads/}
# Asks for deletion of every branch besides master or current branch
if [[ $branch != 'master' && $branch != $current_branch ]]; then
read -q "answer?Delete '${branch}' branch? [y/n]: "
if [ $answer = 'y' ]; then
print ""
git branch -D $branch
else
print ""
fi;
fi;
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment