Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cherry-pick a merge

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?)

like image 999
readonly Avatar asked Oct 24 '08 07:10

readonly


People also ask

How do you cherry pick a merged pull request?

You can cherry-pick merge requests from the same project, or forks of the same project, from the GitLab user interface: In the merge request's secondary menu, select Commits to display the commit details page. Select the Options dropdown and select Cherry-pick to show the cherry-pick modal.

Do we need to cherry pick merge commit?

So when we are cherry-picking the specific commit that was a merge with two parents, we need to tell the cherry-pick command which one against which the diff should be used by using the -m option. We will mention the specific parent branch with the commit hash in the following command.

Is it possible to get merge conflict during git cherry pick?

Yes, at least with the standard git setup. You cannot cherry-pick while there are conflicts. Furthermore, in general conflicts get harder to resolve the more you have, so it's generally better to resolve them one by one. That said, you can cherry-pick multiple commits at once, which would do what you are asking for.


1 Answers

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!

like image 68
Greg Hewgill Avatar answered Sep 17 '22 17:09

Greg Hewgill