Practical Web Programming

Tuesday, March 25, 2014

Git: Delete All Local Branches That Were Already Merged to Master

In Git, to clean up your local repository of already merged branches to master (aka pruning), run this command:

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

This is three commands in one, made possible via pipes ("|").

1) git branch --merged master     # list all the branches that have been merged to master
2) grep -v "\* master"                 # select all the branches that does not match "* master"
3) xargs -n 1 git branch -d          # execute git branch -d with 1 parameter - the branches that was returned by #2


Recent Post