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

Better Stack Team
Updated on June 24, 2024

To delete all Git branches that have been merged into the current branch, you can use the following command:

git branch --merged | grep -v "\\*" | xargs -n 1 git branch -d

This command performs the following steps:

  1. git branch --merged: Lists all branches that have been merged into the current branch.
  2. grep -v "\\*": Excludes the current branch from the list.
  3. xargs -n 1 git branch -d: Deletes each branch listed.

After running this command, all merged branches (except the current one) will be deleted from your local repository.


  • Be cautious when deleting branches, as this action cannot be undone.
  • Make sure you are on the branch where you want to keep the changes before running the command.
  • Ensure that you have pushed any important branches to a remote repository before deleting them locally.
  • Branches that have not been merged will not be deleted by this command. If you want to delete unmerged branches as well, you can use git branch -D instead of git branch -d, but be aware that this will delete branches regardless of their merge status.
Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github