How to reparent in Git

These all look like applications of git rebase --onto.

1a. Reparenting I

Before:

A---B---C---D

After:

  C'---D'
 /
A---B

Set up branches to label the particular commits and then rebase onto.

  1. git checkout -b ex1a-b B
  2. git checkout -b ex1a-d D
  3. git checkout -b ex1a-a A
  4. git rebase --onto ex1a-a ex1a-b ex1a-d

1b. Reparenting II

Before:

  C---D
 /
A---B

After:

  C
 /
A---B---D'

Creating branches is similar to the above: git rebase --onto ex1b-b ex1b-c ex1b-d.

1c. Reparenting III

Before:

  C
 /
A---B---D

After:

  C---B'---D'
 /
A

Again with the branches, but now just git rebase ex1c-c ex1c-d.

Leave a Comment