Delete multiple branches in one command

You can use git branch -d or git branch -D and give the list of the branches as an argument to delete all the branches in one command. Alternatively, you can use git branch –list output as an argument instead of manually writing the list of branches.

Let us see it step-by-step.

The initial setup is as shown. We have five branches.

~/repoone$ git branch
* delbranchone

We aim to delete branches firstbranch and secondbranch. We need to checkout into a branch that is not one of these. Here, we checkout to main.

~/repoone$ git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.

You can use git branch -d <list of branches> to delete the two branches. As you can see, git branch shows the two branches firstbranch and secondbranch deleted after the command.

~/repoone$ git branch -d firstbranch secondbranch
Deleted branch firstbranch (was d0978f9).
Deleted branch secondbranch (was d0978f9).

~/repoone$ git branch
* main

Sometimes, it isn’t easy to list all branches manually. You can make use of git branch –list as an argument. Here, we are executing git branch –list on a pattern del*, and we can see that two branches – delbranchone and delbranchtwo are listed.

~/repoone$ git branch --list del*

We can use the above command as an argument to git branch -d, to delete all the branches that match a specific pattern, i.e., del*.

~/repoone$ git branch -d `git branch --list del*`
Deleted branch delbranchone (was d0978f9).
Deleted branch delbranchtwo (was d0978f9).
ubuntu@ip-172-31-0-61:~/repoone$ git branch
* main