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.

Note:

  • 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
Git
Licensed under CC-BY-NC-SA

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