>_ IzzyLab

Hands-on task

This task is step 9 of the course “Git: rewriting and rescuing history”

Transplant commits with rebase --onto

hard~7 min

In the /root/repo repository there is a main branch (commits base, m1, m2) and a topic branch that forked off main before m2 and contains three of its own commits: t1, t2, t3 (each adds a file of the same name: t1.txt, t2.txt, t3.txt).

You need to transplant onto the tip of main only the last two topic commits — t2 and t3, dropping the t1 commit entirely. After the operation, the topic branch should sit on top of main (contain m2.txt, t2.txt, t3.txt), and the file t1.txt should not be in it.

What you'll practise:
  • git rebase --onto
  • transplanting commits
  • relative references
  • topic~N
  • branch point

Topic breakdown

A plain git rebase moves the whole series of commits from the branch point onto a new base. The git rebase --onto form breaks that rigid coupling and lets you transplant only the tail of a branch, dropping its lower commits. It takes three reference points — the new base, the upper boundary of the discarded chunk and the branch being moved; the boundary is conveniently given by a relative reference like <branch>~N.

What's checked

  1. topic transplanted onto main
  2. commit t1 dropped
  3. commit t2 transplanted
  4. commit t3 transplanted
  5. exactly two commits in topic on top of main

What it looks like