Cleanup Git Branches Already Merged into Master

Here are some command line statements to help cleanup your branches.

Clean remote git branches already merged into master

Switch to master, fetch and pull the latest updates from master.

git checkout master && git fetch && git pull

List the branches about to be removed.

git branch --remote --merged master | grep -v 'master$' >/tmp/merged-branches && sed -i.bak 's/origin\///g' /tmp/merged-branches && vi /tmp/merged-branches && xargs git push origin -d </tmp/merged-branches

Screen will display vi with list that can be edited and then saved with “:wq” to complete the delete

Clean local git branches already merged into master

Again, switch to master, fetch and pull the latest updates from master.

git checkout master && git fetch && git pull

Clean all local git branches that determined to have already been merged into master.

git branch --merged master | grep -v 'master$' | xargs git branch -d

Clean local git branches no longer are on the remote

git remote prune origin

Leave a Reply

Your email address will not be published. Required fields are marked *