How to delete all local branches except master and develop in one command without aliases?

When working on projects with Git Flow or similar workflow where more than one stable branch exists I create a lot of feature branches (feature/do-something-1, hotfix/fix-bug-1, etc.).

Sometimes I need to clear the list of local branches because it is literally impossible to manage them with dozens of rudimentary branches. I usually delete them one at a time by copying and pasting the branch names into the git branch -d command. But it takes so long that it’s easier to delete the entire repository and clone again.

I want to delete all of them except master and develop in one command (without writing additional shell script/aliases), but all solutions I’ve found on the internet only allow you to delete everything except one branch (master, for example). This is not an appropriate solution. Has anyone faced a similar problem?

>Solution :

 git branch | grep -v " master$" | grep -v " develop$" | xargs git branch -D

Leave a Reply