19

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
James Tauber
  • 3,386
  • 6
  • 27
  • 37
  • 1
    Your questions sound like quiz questions ... are you sure you are allowed to simply ask on the internet? – Paŭlo Ebermann Feb 12 '11 at 23:01
  • 11
    Paul, what class even knows what version control is, let alone put git graph node samples on their tests? – ironfroggy Feb 12 '11 at 23:12
  • 1
    *Quiz* does not necessarily have to be a class ... I don't know, but the question(s) simply have so little context that they really sounded like this. – Paŭlo Ebermann Feb 13 '11 at 15:06
  • 5
    why is a context necessary in this case? I deliberately broke the problem down in to the very specific operations I was trying to do. – James Tauber Feb 14 '11 at 01:54

2 Answers2

19

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Emil Sit
  • 22,894
  • 7
  • 53
  • 75
7

It depends on your definition of B', C', and D'.

If you want to move around patches, so that (in the first example) git diff B C == git diff A C', then you want to "rebase" (not "reparent") using git rebase as described by Emil.

If, on the other hand, you want to move around snapshots, then you really do want to "reparent". To do this, I suggest using git reparent.

Most likely, you actually want to rebase and not reparent, but for reference, here are the commands for reparenting. They are somewhat complicated, because this is not a normal thing to do.

(The following assumes you are currently in a branch at D.)

1a. Reparenting I

git rebase -i B
# Select the "edit" command for C
git reparent -p A
git rebase --continue

1b. Reparenting II

git reparent -p B

1c. Reparenting III

git rebase -i A
# Select the "edit" command for B
git reparent -p C
git rebase --continue
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mark Lodato
  • 50,015
  • 5
  • 41
  • 32