11th June 2024

delete feature branches

Quite often I forget to delete feature branches that have already been merged from my local environment. They eventually pile and deleting them one by one is quite a task. A solution I found was to create aliases in my .zshrc file to handle this for me.

#delete all branches except master
alias git-clean-master='git branch | grep -v "master" | xargs git branch -D'

#delete all branches except main
alias git-clean-main='git branch | grep -v "main" | xargs git branch -D'

#delete all branches except dev
alias git-clean-dev='git branch | grep -v "dev" | xargs git branch -D'

I've worked on multiple projects all having different default branches. master, main and dev are the common ones, so I created three aliases to handle those.