10

Let's say branch B is a topic branch off of branch A, and you want those changes in branch C. What does it mean when you cherry-pick the merge commit of branch A and branch B to branch C?

For example, if you use the -m flag to specify the old HEAD of branch A to cherry-pick the merge to branch C, does that simply mean "Take the diff between the cherry-picked commit tree and old HEAD of branch A and apply it to branch C?"

Are there any gotchas for using this method? (e.g. Would branch C look like it's merged to branch A and B? Would more changes be applied than simply the commits from branch B?)

1
  • Yes -m1 will take the diff between the first parent (`^1) of the merge commit and the merge result. The gotchya of cherry-picking (or reverting) merges is that git will not understand that this has anything to do with a merge or branch B. To git it will just be a new commit with certain changes. Be prepared for merge conflicts when merging such branches together at a later point of time.
    – Jay
    Apr 22, 2022 at 17:59

1 Answer 1

9

The way I usually do this is using git rebase:

git rebase --onto C A B

This takes the diffs between A and B, and applies those diffs to branch C. As a bonus, the rebase will skip any commits between A and B that perform the same textual change as already exists in branch C.

Update: In the case you mentioned in the comments, remember that Git never overwrites past history. So even after doing the rebase above, you could recreate a new branch head at the commit where B used to be before the rebase. Unfortunately I can't think of a simple way to do that at this time of the morning. Sorry I couldn't be more help, perhaps somebody else will come up with an easy way!

3
  • Sorry, I left out that I wanted the changes in branch B to be applied to both A and C. The rebase would cause the changes in B to be easily merged with C, but now A is in the situation that C was in. Anything I can do to have those changes in A as well?
    – readonly
    Oct 24, 2008 at 9:10
  • I'm also sorry that I chose such poor branch names when posing the question :(
    – readonly
    Oct 24, 2008 at 9:11
  • git rebase works best with -i (= --interactive) in my opinion. It let's you see/modify which commits will be rebased.
    – Jay
    Apr 22, 2022 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.