Skip to content
Salatiel Queiroz
DibbbleLinkedIn

How to delete multiple git branchs at once

Git1 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 branchs
2git branch
3
4# Local branchs
5main*
6prefix-branch1
7prefix-branch2
8prefix-branch3
9
10# delete all branchs with the 'prefix-' in their names
11git branch -D `git branch --list 'prefix-*'`
12
13# Check the list again
14git branch
15
16# Branchs deleted
17main*

This works with any kind of pre or posfix too.

© 2024 by Salatiel Queiroz. All rights reserved.
Theme by LekoArts