Search code examples
gitgit-rebase

Interactive Rebase of merged branch


I'm using git and I'm getting to this state:

      X --- Y --------- M1 -------- M2 (my-feature)
     /                 /           /
    /                 /           /
   a --- b --- c --- d --- e --- f (stable)

This happen when we work on 'my-feature' branch for more than a day. M1 and M2 are merged done from the stable branch to the feature branch. M1 and M2 may had merged conflicts which have been resolved. The whole idea of merging the stable branch into the feature branch is to handle the conflicts sooner rather than later.

Once the feature is complete we want to rebase the feature branch into one or two commits.

The problem is when we do interactive rebase git show us the same merge conflicts we already solve during M1 and M2 merges.

Is there way to make git reuse the merge decision we already done in M1 and M2 ?


Solution

  • If the merge conflicts are identical, this is a perfect use case for git rerere, one of the most useful (albeit lesser-known) commands in git. From the man pages:

    In a workflow employing relatively long lived topic branches, the developer sometimes needs to resolve the same conflicts over and over again until the topic branches are done (either merged to the "release" branch, or sent out and accepted upstream).

    This command assists the developer in this process by recording conflicted automerge results and corresponding hand resolve results on the initial manual merge, and applying previously recorded hand resolutions to their corresponding automerge results.

    git rerere keeps a record of your conflict resolutions and applies them automatically when encountering the same conflict in git merge, git rebase, or git commit (when committing a merge). Scott Chacon posted some great examples here, and the man page is also worth a read.

    You can enable git rerere in merge and rebase by issuing this command:

    git config --global rerere.enabled true
    

    Remove the --global flag if you'd like the option enabled for just a single repository.