Git

How do I Delete All Git Branches Which Have Been Merged?

While working on a Git development project, developers merge one branch with another. A single repository contains multiple merged branches. However, some merged branches are old and unused and take a lot of disk space. Therefore, such branches must be deleted from the Git repository, as deleting merged branches can reduce the repository’s size and the project history.

This write-up will explain the method of deleting all branches which have been merged in Git.

How to Delete/Remove All Branches Which Have Been Merged in Git?

To delete or remove all Git branches that have been merged, follow the provided steps:

  • First, navigate to the local repository.
  • Then, view the merged branches using the “git branch –merged” command.
  • After that, run the “git branch –merged | egrep -v “(^\*|master)” | xargs git branch -d” and specify the branch that you want to keep. This command will delete all the branches that are merged except the “master” branch.
  • Lastly, verify changes by viewing the list of all branches.

Step 1: Navigate to Local Repository

First, run the below-listed command and redirect to the particular local directory:

$ "C:\Git\ReposC"

Step 2: View List of Available Branches

Then, display the list of all available branches in the repository through the following command:

$ git branch

It can be observed that the repository contains three local branches, i.e., “alpha”, “beta”, and “master”:

Step 3: View Merged Branches

Next, type out the “git branch” command with the “–merged” option to display only the merged branches:

$ git branch --merged

The below output indicates that the “alpha” branch has been merged with the “master” branch:

Step 4: Delete Merged Branches

Now, enter the below-provided command to delete the merged branches and specify the branch which you want to keep. For instance, we have specified the “master” branch because we want to keep it:

$ git branch --merged | egrep -v "(^\*|master)" | xargs git branch -d

Here:

  • git branch –merged” displays all branches that have been merged with the current working branch.
  • egrep -v “(^\*|master)”” filters the list of branches except for the “master” branch.
  • xargs git branch -d” deletes/removes the other merged branches.

The below output indicates that the merged branch “alpha” has been deleted successfully:

Alternatively, users can simply delete any branch using the “git branch -D <branch-name>” command.

Step 5: Verify Changes

Lastly, view the list of all available branches to verify changes:

$ git branch

It can be seen that now the repository contains only two branches, i.e., “master” and “beta”, and the “alpha” branch has been deleted from the repository:

We have explained the procedure to delete all branches which have been merged.

Conclusion

To delete or remove all Git branches that have been merged, first, move to the local repository. Then, view the merged branches. Next, execute the “git branch –merged | egrep -v “(^\*|master)” | xargs git branch -d” and specify the branch that needs to be kept. Lastly, verify changes by viewing the list of all branches. This write-up explained the method of deleting all branches which have been merged in Git.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.