Martijn Courteaux
Martijn Courteaux

Reputation: 68847

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?

Upvotes: 4

Views: 753

Answers (1)

torek
torek

Reputation: 488003

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.

Upvotes: 3

Related Questions