Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reparent in Git

Tags:

git

What non-interactive Git command(s) achieve the change from Before to After in each case?

1a. Reparenting I

Before:

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

After:

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

1b. Reparenting II

Before:

  C---D
 /
A---B

After:

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

1c. Reparenting III

Before:

  C
 /
A---B---D

After:

  C---B'---D'
 /
A
like image 824
James Tauber Avatar asked Feb 12 '11 22:02

James Tauber


1 Answers

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.

like image 160
Emil Sit Avatar answered Oct 15 '22 00:10

Emil Sit