Skip to content

Instantly share code, notes, and snippets.

@PierreTurnbull
Last active October 14, 2020 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PierreTurnbull/3b8a2f2fa7c8e7bea28634e6b0c83dcd to your computer and use it in GitHub Desktop.
Save PierreTurnbull/3b8a2f2fa7c8e7bea28634e6b0c83dcd to your computer and use it in GitHub Desktop.

Initial situation

We need to merge branch1 and branch2 on master. First, we'll update each branch from master in order to fix potential conflicts. Then we'll merge the branches on master.

          7 - 8 branch2
         /
1 - 2 - 4 - 9 master
     \
      3 - 5 - 6 branch1

Resolving conflicts by merging

Merging master on branch1 and branch2

          7 - 8 - 10 branch2
         /       /
1 - 2 - 4 - 9 --- master
     \           \
      3 - 5 - 6 - 11 branch1

Merging branch1 and branch2 on master

          7 - 8 - 10 branch2
         /       / \
1 - 2 - 4 - 9 ----- 12 - 13 master
     \           \      /
      3 - 5 - 6 - 11 --- branch1

Resolving conflicts by rebasing

Rebasing branch1 on master

          7 - 8 branch2
         /
1 - 2 - 4 - 9 master
     \       \
      \       3'- 5'- 6' (branch1)
       \
        3 - 5 - 6 (detached)

Merging branch1 on master

          7 - 8 branch2
         /
1 - 2 - 4 - 9 --------- 10 master
     \       \         /
      \       3'- 5'- 6' (branch1)
       \
        3 - 5 - 6 (detached)

Note that if merging locally, since branch1 and master are up to date, there will be a fast-forward, and commit 10 won't be created. That being said, a PR will never use fast-forward.

Rebasing branch2 on master

          7 - 8 (detached) 7'- 8' branch2
         /                /
1 - 2 - 4 - 9 ---------- 10 master
     \       \          /
      \       3'- 5'-- 6' (branch1)
       \
        3 - 5 - 6 (detached)

Merging branch2 on master

          7 - 8 (detached) 7'- 8' branch2
         /                /     \
1 - 2 - 4 - 9 ---------- 10 ---- 11 master
     \       \          /
      \       3'- 5'-- 6' (branch1)
       \
        3 - 5 - 6 (detached)

Conclusion :

The result history looks lik this:

When resolving conflicts by merging

          7 - 8 - 10 branch2
         /         \
1 - 2 - 4 - 9 ----- 12 - 13 master
     \                  /
      3 - 5 - 6 - 11 --- branch1

Branches overlap

When resolving conflicts by rebasing

                           7'- 8' branch2
                          /     \
1 - 2 - 4 - 9 ---------- 10 ---- 11 master
             \          /
              3'- 5'-- 6' branch1

Branches follow each other

Rebasing is way cleaner. It enables to go back in history with simplicity and accuracy that merging doesn't offer. By experience, tracking a bug in the history is a tedious task when branches overlap.

Also, rebasing makes conflict resolution safer, since it requires you to fix conflicts commit by commit rather than all at once. Therefore, it is easier to determine which changes to keep.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment