How to Deal With Stale Branches on the Github

A guidance how to keep your git branches list clean

Vladyslav Babak
4 min readNov 13, 2019
github branches

In this article I want to show you a way how to deal with stale git branches on the Github.
I suppose you are already familiar with git and Github, so it will be easy to read and understand information below.
I will try to keep this article short and my thoughts and text clear.

Let’s begin!

"Stale branch"? what is that?

Let's imagine you have a branch "feature_123". You made commits in it but for some reason the work was stopped. Is it a stale branch? If so, since what time?

"Stale branch" is a git branch without commits for the last few months.
For the Github, such period of inactivity is 3 months.

You can check now your project’s repository "branches" tab and see if you have such branches.

android/architecture-samples
android/architecture-samples

Why do I have those branches?

There could be a lot of reasons to have such non-supported branches. A few of them are:

  • unreleased, a feature was implemented but not included into the final version of the product
  • prototype, like a proof-of-concept, a feature was done to confirm some statements about the idea
  • alternative solution, yet another possible implementation (most likely not the best)
  • incomplete, a feature that was started a long ago and was stopped unfinished or was postponed
  • incompatible, a feature became incompatible with the rest of the project (e.g. technology stack was updated)

It's okay if your project does not have such branches at all. But if it has, I would be happy to share my notes of how to deal with it.

Keep or delete?

Keeping such branches may lead to a list, difficult to work with.
Deleting a branch isn't suitable in the case you will want to restore it easily. The only way to restore the code will be to checkout by a commit hash.

git checkout -b <branch> <hash>

Can’t we just archive it?

Well, there is no such option like archiving branches in git. But we can create a tag with a prefix like “archive”.

git tag archive/<branchname> <branchname>
git branch -d <branchname>

In other words, we move entry about the feature from branch list to tags list.
To restore the branch, checkout it by the tag.

git checkout -b <branchname> archive/<branchname>

Cancelled pull request

There is another option, I wish to tell you about, which gives you more benefits such as:

  • keep repository clean
  • won’t merge the code into the master branch
  • delete branch
  • easily find and restore deleted branch

Step 1. Create a PR

Go to the the "New pull request" page on the Github and create a pull request.
It does not matter if there will be a merge conflict, just don't resolve it. You will save your time.

New PR
New PR

Step 2. Set the right name

Name your pull request exactly the same as your source branch.
Example: “feature_123.

Step 3. Cancel this pull request

Yes, don't merge it! Scroll down and find "Close pull request". Close it without merge.

Cancel PR
Cancel PR

Step 4. Delete the branch

Now you can safely delete the branch.

Delete a branch
Delete a branch

Step 5. How to restore?

At any time, when you will need it, simply find your closed pull request on the "Pull requests" page by the filter: “head:feature_123”.

Filter PR by branch name
Filter PR by branch name

Open the pull request page and press "Restore branch" button.
All the working process in the branch was successfully restored!

Restore branch
Restore deleted branch

Most of branches in the code repository are reflecting features or tasks to be done. And in today’s fast changing business requirements environment, number of branches constantly keeps growing. Having a possibility to delete a branch and reactivate it on demand helps to keep your repository up-to-date and clean.

References

https://help.github.com/en/github/administering-a-repository/viewing-branches-in-your-repository
https://confluence.atlassian.com/bbkb/how-to-restore-a-deleted-branch-765757540.html
https://github.com/vbabak/restore-deleted-branch

--

--