See all articles
Cleaning git repository from merged branches - Developer’s Notes

Cleaning git repository from merged branches - Developer’s Notes

While writing your app, you’ll likely need to delete old, merged code branches. It’s important to maintain a healthy, clean codebase, which will boost your development team’s productivity. They won’t need to wade through obsolete branches in search of the right one, and they’ll be much less likely to make costly mistakes.

Here’s my one-line script for removing merged branches from remote (run it when you are on the latest `master` branch):

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

It simply lists all remote branches that have been merged to your current branch (that's why you need to be on `master`), skips `master` itself (`grep -v master`), removes "origin/" suffix and then passes everything to `git push --delete origin`.

Today’s Developers’ Notes’ tip were shared by Łukasz, Ruby on Rails developer. Stay tuned for more tidbits of knowledge in the near future!

Read Similar Articles