git - merge difference of branches

Tag: git , merge , git-rebase Author: liuliang159 Date: 2011-09-13

I have three branches A, B and C. B is regularly merged into C.

          o---o---o A
--------o---o---o---o---o---o B
         \       \       \   \
          o---o---o---o---o---o C

Now I want to merge the changes I did in C, but without the merges from B, on top of A. What is the easiest way to do this in git?

To which branch would you like to merge it?
he wants it on top of A
Wouldn't that lead to a situation where you have commits Q, W (branch C), E, R (branch B) -- merge -- T, Y (branch C) and just selecting Q, W, T, Y from branch C will break things due to missing code from E, R (which probably T, Y base on)?
@MarcinGil: It might. Depends on what the changes were.

Best Answer

Use the git rebase.

First, rebase your C on top of B:

git checkout C
git checkout -b rebasedC #Let's do a new branch for it also, just in case
git rebase B

it will place all C commits on to of B. Now we want transplant branch rebasedC from B to A:

git rebase --onto A B rebasedC

So, now you have your C-commits on top of A in the rebasedC branch. Now you can fast-forward your A to it:

git checkout A
git merge rebasedC
git branch -d rebasedC# I don't think you would need it.

That's all, I hope.


how does this get rid of B merges made in C?
Rebase does it. Then you move commits on top of B, obviously the C doesn't need the merges anymore, so they are dropped.
I've tried it with my git, now I have the tree structure: (the A branch here is named as 'master' instead).
You are welcome, I love the git features. Also, accept the answer to close the question.
Good answer, but the git rebase B is redundant. The second rebase will also remove the merge commits if you skip the first rebase. It might be a little easier to resolve merge conflicts in two stages, though.

Other Answer1

If i understand correctly, you want to take some commits from C into A.

If that´s the case, why don´t you "cherry-pick" them? It can lead to conflicts, but i think its your best chance :)

Other Answer2

You can try cherry-picking C's non-merge patches. Be prepared to handle merge conflicts. :)