Cleaning git repository from merged branches - Developer’s Notes

iRonin IT Team - Experts in software development
best practices, developers 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!

Author's Bio
iRonin IT Team

Experts in software development

We are a 100% remote team of software development experts, providing web & mobile application development and DevOps services for international clients.

Similar articles
Comments

Bulletproof your development with remote team augmentation

Read how
This page is best viewed in portrait mode
Our websites and web services use cookies. We use cookies and collected data to enhance your experience, provide additional communication channels, improve marketing materials and enhance our offer. IRONIN SP. Z O.O. SP. K. is committed to protecting all the data that we collect or process in any way, especially data of personal nature. By accepting these terms you agree to our usage of cookies and processing your data, according to our Privacy Policy, and you declare that your browser settings reflect your preferences. Read more You have the right to revoke this agreement at any time, based on the terms of our Privacy Policy. You can change cookies settings in your browser. If you do not agree with us using cookies and processing your data, please change your cookies settings in your web browser and reject these terms. You can find more information about cookies, your data privacy This site uses cookies. By continuing to browse the site, you are agreeing to our use of cookies. data processing, and your rights in our Privacy Policy.