DEV Community

Discussion on: What dev topic do you think you should understand, but don't?

Collapse
 
swaranga profile image
Swaranga

Git rebase - when I have currently checkout out branch1 and I do git rebase branch2, please explain which branch's code is copied and applied into which branch.

Collapse
 
phallstrom profile image
Philip Hallstrom • Edited

So, you're working on branch2 and you make commits, A, B, and C. You create branch1 and make commits D and E. You checkout branch2 and make commits F and G. You checkout branch1 and rebase branch2.

This means that you will make it so that branch1 contains all of the changes on branch2 plus the changes you made at the point you created the branch.

So originally, branch1 would be A, B, C, D, E. And branch2 would be A, B, C, F, G. After rebasing branch1 would be A, B, C, F, G, D, E.

In my head I think of it as git going back through your commits till it finds the point you branched, applying all the commits from the specified branch, then re-applying all of your branch's commits.

P.S. git help rebase has a really nice illustration of this I now realize.

   Assume the following history exists and the current branch is "topic":

                 A---B---C topic
                /
           D---E---F---G master

   From this point, the result of either of the following commands:

       git rebase master
       git rebase master topic

   would be:

                         A'--B'--C' topic
                        /
           D---E---F---G master