Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: cherry pick inserts changes from other commits

I've been trying to cherry pick a specific commit from one brach into another. Lets say my history looks like this:

 A - B - C - D   (master)
      \
       X - Y     (feature)

Now, let's say I want to apply only the changes commit D on master to the feature branch, right after Y. When I try to do this, there are merge conflicts and I can see traces of commit C when merging, although I wanted only to cherry pick commit D.

What am I missing?

like image 314
Martijn Courteaux Avatar asked Sep 13 '25 12:09

Martijn Courteaux


1 Answers

git cherry-pick master or git cherry-pick <id-of-D> does a diff from C to D and then attempts to apply that diff to your current tree/commit (in this case, the tree for Y). You will, as you noted, "see traces of commit C" if D makes changes to, or near, things that were also changed in C, because the diff carries context. It's that context that allows git to find what to change in Y, in case lines, or functions, or entire files, have been moved (e.g., in X and/or Y).

Git does not understand semantics, only syntax: it will only cherry-pick cleanly if the syntax matches up, and it will require help from you (and not know what not to show you) if not.

like image 143
torek Avatar answered Sep 16 '25 03:09

torek