1 min read

How To Delete Multiple Git Branches

Tired of deleting many of your unused local git branches one-by-one? Choose the lazy way! Here is a tip on how to delete multiple git branches by a pattern quickly.
How To Delete Multiple Git Branches

Since Git doesn't have a built in feature to delete multiple git branch in local project, deleting many branches in your local Git project might take you a while.

[email protected]:~/go/src/github.com/masgar/test$ git branch
  feat/api-create-product
  feat/api-delete-product
  feat/api-get-products
  feat/api-update-product
* master

The common method is to delete one by one:

git branch -D feat/api-create-product
git branch -D feat/api-delete-product
git branch -D feat/api-get-product
git branch -D feat/api-update-product

While it is easy to do in smaller number of branch, It might turned into disaster when there are too many branches to delete. Here is a tip on how to delete multiple git branches by a pattern quickly.

  1. Open a terminal, move to your Git project.
  2. Type git branch | grep "<pattern>" to see list of git branch
  3. Type in git branch | grep "<pattern>" | xargs git branch -D

Change the <pattern> with a regular expression(RegEx) that match your branch name. For example: git branch | grep "feat" | xargs git branch -D or git branch | grep "*" | xargs git branch -D

Credit to Raja Sekar Durairaj for posting Deleting Multiple Branches in Git in Medium