How to delete merged branches on Git and GitHub

author image
By Ben Radler

Recently at OneLogin we decided to prune and purge all merged branches in one of our large Git repos. Having never had to do this with more then a handful of branches, I had relied on the GitHub branches UI.

This UI is less than ideal when working with over about a dozen branches, so I started looking into scripts to help automate the process.

The best information I found was in this StackOverflow question.

I'll cut right to it:

Delete branches not merged into currently checked out branch on LOCAL

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

Prune your origin

git remote prune origin

Delete branches not merged into currently checked out branch on ORIGIN

git branch -r --merged | grep -v master | sed 's/origin\//:/' | xargs -n 1 git push origin
author image

Ben Radler

Ben is a Software Engineer. He works on autonomous vehicle dispatch at Cruise.

© 2024 benradler.com. I love you.