DEV Community

Aaron Powell
Aaron Powell

Posted on

How to delete all local git branches merged into the current branch

I saw Ben Halpern's post recently on how to remove all local git branches and thought I'd share a useful snippet I have as an alias for deleting all merged branches:

[alias]
    rmb = "!f() { git branch --merged | grep -v "master" | while read i; do git branch -d $i; done; }; f"
Enter fullscreen mode Exit fullscreen mode

rmb is short for remove merged branches and it'll go over all branches that have been merged into the current branch you are on and remove them.

I'll often run git rmb on master to do a cleanup, but it can be done on any branch that has had sub-branches off it too. 😊

Top comments (1)

Collapse
 
malilevi1 profile image
malilevi1 • Edited

I am using the following to remove all remote branches which were marged in master. Usually I do this 1x in a month to cleanup the repository for already merged branches.
git branch -r --merged master | cut -d/ -f2- | xargs -n 1 git push --delete origin

The first part is to get all merged remote branches, then remove the origin/, then pass the result and execute the delete command.