Git

Can You Delete Multiple Branches in One Command With Git?

Multiple Git branches are used for each module while developing large projects that contain the source code files. When users finish work on them, they are required to remove branches from the repository. Moreover, developers can delete multiple branches at once by utilizing the “git branch -d” command along with the “git branch –list ‘<prefix *>’” option.

This blog will discuss the method of deleting multiple branches using a single command with Git.

Can You Delete Multiple Branches in One Command With Git?

In order to delete multiple branches at once, first, move to the Git particular repository and check the list of all branches. Then, remove multiple branches simultaneously by running the “git branch -d ‘git branch –list ‘<prefix *>’” command.

Now, move forward and check the results of the above-discussed command!

Step 1: Redirect to Desired Repository

At first, navigate to the particular repository by running the provided command:

$ cd "C:\Users\nazma\Git"

Step 2: View Local Branches List

Then, execute the “git branch” command to check the existing local branches:

$ git branch

Here, the below-highlighted branches are selected for removing at once:

Step 3: Delete Multiple Branches

Now, delete the selected branches through the “git branch” command:

$ git branch -d `git branch --list 'de*'`

In the above-stated command:

  • -d” option represents the delete.
  • –list” option is used to provide the list of branches that need to be deleted.
  • de” is used as the prefix of those branches whose name starts with “de” and is required for deleting.
  • Asterisk “*” symbol utilized for all.

As you can see, all branches which have the “de” prefix are deleted from the local repository at once:

Step 4: Ensure Deleting Procedure

Lastly, execute the “git branch” command to verify if the branches are deleted from list or not which contain the specified prefix:

$ git branch

It can be observed that multiple branches are deleted successfully:

That’s all! We have provided the method of deleting multiple branches simultaneously with Git.

Conclusion

To delete multiple branches at once, first, move to the Git particular repository and check the list of all branches. Then, execute the “git branch -d ‘git branch –list ‘<prefix *>’” command to remove multiple branches simultaneously. Lastly, verify the deleting operation. This blog explained the method of deleting multiple branches using a single command with Git.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.