Deleting already merged local git branches

Max Kimambo
Thoughts of a software fundi
1 min readApr 25, 2017

--

Recently I have been working on refactoring an existing code base, which resulted
in quite a number of pull requests each of which originated from
own branch, which of course after merge meant that the remote branches were deleted, but I was then left
with all this local branches which is quite messy, you might agree.

This is how it looked like.

zuora Max$ git branch -l  
dev_zuora
feature_99_cent_license
feature_factoring_redis_queues
* feature_refactoring
feature_refactoring_invoice
feature_refactoring_job_queues
feature_refactoring_product
feature_refactoring_remove_gulp
feature_refactoring_transactions
master
poc
refactoring-actions

So I decided to delete everything that had already been merged into feature_refactoring branch.

This can be achieved with a bit of console trickery.

By listing all the merged branches to a given branch in this case it was
feature_refactoring branch and then grepping and passing this as an argument to git branch -D command.

The final command looked like this.

git branch --merged feature_refactoring | grep -v "\* feature_refactoring" | xargs -n 1 git branch -d 
Deleted branch feature_factoring_redis_queues (was 68ae57b).
Deleted branch feature_refactoring_job_queues (was 881b9c9).
Deleted branch feature_refactoring_product (was cece425).
Deleted branch feature_refactoring_remove_gulp (was 6f8e678).
Deleted branch feature_refactoring_transactions (was 9c95708).
Deleted branch refactoring-actions (was 65200c8).

Now we are only left with branches that I still need in the project.

--

--