How to Manage a Long-Lived Git Branch with Rerere

Large software projects with hundreds of developers contributing new features and bug fixes face more than development challenges. Today, I’ll talk about something often overlooked: Git management during long-running releases.

Let’s imagine a starting scenario with a feature branch that has been under development for a couple of weeks, with some features already implemented, and a master branch where fixes and improvements have been added, as shown in the following diagram:

Example of a long-term branch

At this point, merging the feature branch into master would be easy, although we might have to resolve a few conflicts. Now imagine that the feature is a deep structural change and takes nearly 10 months to complete, while the master branch continues to receive updates and bug fixes.

We’re responsible for releasing the new version and now face a merge after a 10-month split between the branches. Can you see the problem ahead? There will probably be hundreds of conflicts to fix—a very tedious process, though not an impossible one.

So the question is: Can we avoid this situation somehow? The first idea that comes to mind is to keep merging master into the feature branch, which would add hundreds of extra commits. The second would be to rebase on the branches mentioned above, which is completely unfeasible because it would rewrite public history and break Git’s golden rule.

Out of ideas? Here’s some good news: Git gives us a lesser-known tool that makes this scenario trivial.

The magic option—which isn’t really magic—is called “reuse recorded resolution,” better known by its acronym, rerere. To enable it, change the following configuration:

$ git config --global rerere.enabled true

Once enabled, Git will automatically save and reuse conflict resolutions. The process for solving the problem we described is then:

  • Merge the feature and master branches occasionally (git merge).
  • Resolve the conflicts (the resolutions will be saved).
  • Revert the merge. ($ git reset --hard HEAD^)

Now imagine it’s time for the final merge. Following the usual process, rerere can apply the resolutions made over the previous months and turn what used to be an endless, risky task into a matter of minutes.

Related posts