Nicolas Assouad
Software engineer and Entrepreneur
CV Github LinkedIn
Tutorial: git rebase --onto
2020/02/17
#tutorial

Let's consider the following scenario: we have a main branch, main, and we have worked on a new feature by breaking it down into two successive branches, featA and featB.

main        featA   featB
 |           |       |
 v           v       v
 ca--c1--c2--c3--c4--c5

Commits have been added to the main branch, and we would like to rebase our branches onto the new version of main.

         main
          |
          v
ca--cb--cc

To do this, we will perform two successive rebases: a simple one first, followed by one using the --onto command.

git rebase main featA

git rebase --onto featA c3 featB

The --onto command allows us to rebase a range of commits. Here, we are rebasing the commit range from c3 (exclusive) to featB onto featA, which includes commits c4--c5. By comparison, a simple rebase would have also rebased the commits from the old featA branch, resulting in duplicate commits c1--c2--c3.

We finally obtain the following commit tree:

         main          featA     featB
          |              |         |
          v              v         v
ca--cb--cc--c1'--c2'--c3'--c4'--c5'

EN FR