In this article you learn that how to delete all branches that are merged to the master branch
First, list locally-tracking branches that were merged in remote (consider using -r
flag to list all remote-tracking branches).
git branch --merged
You might see few branches you don't want to remove. We can add few arguments to skip important branches that we don't want to delete like master or a develop. The following command will skip master branch and anything that has dev in it.
git branch --merged| egrep -v "(^\*|master|main|dev)"
To delete all Git branches which have been merged, you can use the following steps:
git branch --merged | grep -v "\*" | xargs -n 1 git branch -d
This command tells Git to list all branches that have been merged into the current branch (represented by the HEAD
pointer), exclude the current branch from the list, and then delete all the remaining branches one by one.
Note: The git branch -d
command is a powerful tool, and it can permanently destroy branch information if you're not careful. Before running this command, make sure you have saved any important changes in the branches that you're about to delete, or that you're willing to lose them.
After running the above command, all branches that have been merged into the current branch will be deleted, leaving only the current branch and any unmerged branches in your Git repository.