How to delete all Git branches which have been merged

Building a program can be a very tiring and time-consuming task. Deleting branches that have been merged into the main is inevitable.

Let’s discuss the steps required to delete branches that have been merged.

Implementation

Deleting branches that have been merged is an easy task. You can do this in your command prompt, terminal (for mac users), or Git bash.

Note: Git Bash is an application for Microsoft Windows environments that provides an emulation layer for a Git command line experience.

Below are the steps to delete Git branches that have been merged:

  1. List all the branches that have been merged: The first step to delete all Git branches which have been merged is to list the branches. Run the following code:

git branch --merged

  1. Delete all merged branches: To delete all the Git branches that have been merged, run the code below:

git branch --merged | grep -i -v -E "master|important"| xargs git branch -d

Note: The grep sequence contains the branches we do not want to delete. The code above excludes the master and any branches that contain importance in its name.

We can run the git branch to check all the branches that remain.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved