How to delete multiple git branchs at once
— Git — 1 min read
Sometimes you forgot to delete you local branchs, which happens to me all the time, and someday
you type git branch
to see you local branchs and you ended up with a huge list.
So the best way would be deleting multiple branchs at the same time to save up some
time writing each branch name one by one.
TLDR;
You can delete multiple branchs by typing each name separated by spaces:
1git branch -D branch1 branch2 branch2
Or if the branchs you want to delete has a shared name pattern you can use the code bellow to delete multiple branchs at the same time:
1git branch -D `git branch --list 'prefix*'`
Example
Deleting multiple branch with the same prefix:
1# List your branchs2git branch3
4# Local branchs5main*6prefix-branch17prefix-branch28prefix-branch39
10# delete all branchs with the 'prefix-' in their names11git branch -D `git branch --list 'prefix-*'`12
13# Check the list again14git branch15
16# Branchs deleted17main*
This works with any kind of pre or posfix too.