4

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?

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287

1 Answers1

3

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.

torek
  • 448,244
  • 59
  • 642
  • 775
  • It literally adds lines of code that were added in commit C. These lines are indeed next to the ones that are added in commit D. Is that the behavior you expect? – Martijn Courteaux Jun 06 '15 at 09:59
  • I'd have to see the actual commit(s) to tell for sure. But, you can `git show` commit `D` to see what git thinks it will need to apply. – torek Jun 06 '15 at 12:06
  • Can't tell anymore. These commits and branches are all replaced or removed. (I really learned some new git skills, thank you very much :) ) – Martijn Courteaux Jun 06 '15 at 12:59