Dealing with Growing Branches

Dale Seo
Prodigy Engineering
5 min readJan 6, 2020
Photo by Brandon Green on Unsplash

Remember the last time you scaled a software project? Everything starts growing: the amount of code, the number of contributors, the number of issues and pull requests and — as many of us have experienced — the number of branches in the code repository.

People working on your project create branches for different reasons. Most developers usually create branches for new features or bug fixes, while others occasionally create branches just for experiments or research and have no plans to merge the code changes.

Plus, automation tools like Dependabot, Snyk and Renovate create branches on your behalf. These tools come with a little bot that’s able to create branches and pull requests as if a developer was doing it.

Since it’s too easy to create new branches off of existing ones (at least if you’re using Git), people and bots don’t hesitate to create new branches and then completely forget about them. This is why it gets more difficult to keep your code repositories clean as your project/team grows.

Ideally, we would always clean up the branches we create once they’re no longer used. In reality, it takes more time and energy than you might expect to ensure all branches are deleted in a timely manner.

Do branches really matter?

That’s a very good question! The answer is yes and no.

Some teams don’t care how many branches they have. However, when your team starts integrating different tools and services with your code repositories, too many branches can get in your way.

At Prodigy we have a cool internal tool that lets you compose an environment from a specified set of Git branches. This makes sharing changes across teams easy. However, it wouldn’t be very pleasant if there were hundreds of branches to choose from for each Git repository.

Those unused branches can also make it difficult to navigate the codebase on a code hosting service like Github. You’ll often run into a lengthy dropdown button for switching branches and feel confused or overwhelmed.

A long dropdown button on Github

Why & how do branches become stale?

Though it depends on the project, here are some common scenarios where branches end up being stale:

  • Feature or bug branches should be deleted by the developer or QA who merged them but they find it cumbersome to do it manually.
  • Temporary branches created for experiments or research are kept because they might be useful one day, but no one ever comes back to them.
  • If pull requests automatically opened by bots are not reviewed, branches associated with those pull requests become outdated.
  • When someone leaves your team/organization, all of their branches become orphans.

If these are happening in your projects, your code repositories will probably be polluted with unnecessary old branches sooner or later. 😱

Developers are busy (and sometimes lazy)

If you are less willing than your teammates to tolerate a significant number of branches on your project, you might consider sending the team an email or posting a Slack reminder for everyone.

A friendly reminder on Slack

This could work for a small team, but it’s not scalable for bigger teams. Some team members would stop working and go to the repository to see if we have any branches. Others who didn’t have the time would put it off and probably not come back to it.

Prodigy has hundreds of code repositories on Github that accumulate branches at a high rate. We needed a more sustainable solution to address the growing number of branches throughout the organization.

Our solution

We wanted to make it easier for developers to identify their stale branches and delete them. That’s how we came up with Branch Cleaner, a Slack app that figures out what branches are stale on Github, reminds developers via Slack and helps them delete these branches on the fly.

With this little Slack bot put in place, developers get a weekly notification of branches that are considered stale. They can then delete them directly from the notification and even encourage each other to delete branches.

Slack messages posted by Branch Cleaner

How does it work?

This simple app connects the Github API and Slack API to:

  • Fetch all branches of a repository from Github.
  • Search for merged/stale branches and order them with the oldest ones first.
  • Post a list of messages to the channel of your choice on Slack, each of which contains information about the unused branch and a delete button.
  • When the delete button on a message is clicked, delete the branch from the Github repository and then update the message and controls on the Slack message.

Open-sourcing Branch Cleaner

For the past few months, we have been using this in-house solution against our busiest projects and it has worked well for us. The number of branches on each code repository has dropped significantly. Not only that, the developers working on those projects have become more aware of stale branches, and we’re more likely to clean up branches as we go.

We are happy to share that Branch Cleaner has been open-sourced for other projects/teams who are suffering from the same issue as us! We look forward to working with the open-source community to keep improving this tool so that it can help more code repositories stay nice and clean in the future.

--

--