ToyElephant ToyElephant - 1 year ago 65
Git Question

Order of commits after rebasing a branch into another using git

I have main branch from which I forked out a dev branch a while ago. I continued my development work in dev branch until now. In the meantime, there are newer commits added to the main branch. Now I want to pick up the changes from main to my dev branch. In particular, I want to rebase my changes in dev branch on top of newer changes in the main branch. These are the steps I took :

git checkout main
git checkout dev
git rebase main

After I went through conflict resolution steps, it looks like the commit from two branches got mixed. Is it how it is supposed to work? I was under the impression that all newer commits from main branch will be at the bottom and the commits of dev branch will be on top of them. Am I misunderstanding something here?

Answer Source

Your understanding is correct.

If you want to reapply the changes from dev branch to main branch (the commits from main branch will be at the bottom and the commits of dev branch will be on top of them), you need the following 2 steps:

1.git checkout dev

2.git rebase main

The rebase working theory like this:

1.find the common ancestor (E in the graph) of dev and main

2.get the diff by comparing the ancestor E and each commit of dev branch (each commit diff saves in different temporary files)

3.Switch to main branch, applying each diff from step2 on main branch one by one

More details in

          A---B---C dev
D---E---F---G   main

D---E---F---G---A’---B’---C’  main

Note: when applying diff on main branch, it may has conflicts. You need to modify the conflict files and use git add to add the modified files, and then use git rebase --continue, so it will applys the next diff on main branch.

Kindly reminder: after you finish rebasing, main branch still point to G (see in the graph) and dev branch point to C’. If you want main branch also point to C’, you can use git checkout main and git merge dev

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download