How “git rebase --onto” works

Thongchai Kolyutsakul on 2022-01-13

A TL;DR guide for git history transplant

Update: check out a new shorter version here!

Documentation

Original documentation:

git rebase [-i | --interactive] [<options>] [--exec <cmd>]
	[--onto <newbase> | --keep-base] [<upstream> [<branch>]]
git rebase [-i | --interactive] [<options>] [--exec <cmd>] [--onto <newbase>]
	--root [<branch>]

My “easy to remember” documentation:

git rebase [--onto <new_base>] [<old_base> [<last_commit>]]

Input

This is the initial git history we will use as example.

A-B-C-D master
   \
    E-F-G feature* (current branch)

Output

Here are different ways to use git rebase --onto. I will show the commands and the resulting history below.

git rebase --onto D
git rebase master
A-B-C-D master
       \
        E-F-G feature*
git rebase --onto C
A-B-C-D master
     \
      E-F-G feature*
git rebase --onto C E
git rebase --onto C F~1
A-B-C-D master
     \
      F-G feature* (E disappeared)
git rebase --onto C E F
A-B-C-D master
     \
      F feature* (E and G disappeared)

That’s it. Enjoy !