what happens with the child branches if a rebase a parent branch but not the child?

Viewed 499

Let's say I have the following

 ------------------Dev
     \   
     Branch1------------------Branch2
            \-----Branch3

and I want to have the features of Branch1 in Dev so I checkout to Branch1 and I do git rebase Dev.

I expect to have the branch Branch1 moved to the top of Dev but what happens to Branch2 and Branch3?

2 Answers

Nothing happens to Branch2 and Branch3, which will remain as they are, the same history. Here are more detailed before and after graphs. Here is the before:

Dev:      A -- B
           \
Branch1:    C -- D
             \    \
Branch3:      \    F
Branch2:       E

And here is the after, once you have run git rebase dev from Branch1:

Branch1:         C' -- D'   (after rebase)
                /
Dev:      A -- B
           \
            C -- D   <-- most likely no branch has a HEAD pointing here at this point
             \    \
Branch3:      \    F
Branch2:       E

The same history should be present for the other two branches, as rebasing only rewrites history for the target branch which you mention.

A branch is attached to a commit.

git checkout Branch1 && git rebase Dev is equivalent to git rebase Dev Branch1.

First, it finds out the commits that are reachable from Branch1 and new to Dev.

Then, it applies these commits onto Dev one by one. Empty commits may be ignored.

Finally, it moves Branch1 to the last new commit.

In the process, Branch2 and Branch3 stay where they are.

Here are the graphs before and after. The images are from learninggitbranch.

Before,

enter image description here

After,

enter image description here

Ignore master on the graph. It's not allowed to delete master in this app. C3 is the commit that's reachable from Branch1 and new to Dev. It's applied onto Dev and C3' is created. Branch1 is moved to C3'. C3 and other commits are unchanged, so are Branch2 and Branch3.

Commands to reproduce the process on learninggitbranch,

git checkout -b Dev
git commit
git checkout -b Branch1 C1
git commit
git checkout -b Branch2
git commit
git checkout -b Branch3 C3
git commit
git checkout Branch2
git commit
git rebase Dev Branch1
Related