How to Remove Multiple Git Branches at Once

by Juan Vásquez, 02 May 2023

Git is a powerful version control system that allows developers to manage their source code effectively. One of the features of Git is the ability to create branches, which allow developers to work on multiple features or bug fixes simultaneously without interfering with each other’s code.

However, when a project has many branches, it can become challenging to manage them effectively.

In this article, we’ll show you how to remove multiple Git branches at once to help you keep your repository tidy.

Removing a Single Git Branch

Before we dive into removing multiple branches, let’s first review how to remove a single branch.

To remove a branch, you can use the following command:

git branch -D branch-name-you-want-to-remove

Removing Multiple Git Branches

Git does not have a native way to remove multiple branches in one single command. However, there is a straightforward process that you can follow to remove many branches at once.

  • Open the terminal.

  • Run the following command to filter the branches you want to remove:

git branch | grep "<pattern>"

Replace <pattern> with a regular expression that matches the branches you want to remove.

For example, if you want to remove all branches that start with “feature,” you could use the following command:

git branch | grep "feature.*"
  • Once you’ve verified that the correct branches have been filtered, run the following command to remove them:
git branch | grep "<pattern>" | xargs git branch -D

This command uses the xargs command to execute the git branch -D command on each filtered branch.

And that’s it! By following these simple steps, you can easily remove multiple branches from your Git repository.