12

I ran into a situation where git cherry-pick X would have some conflicts, but also created extra inserts (when verified with git diff).

I then re-ran git show X > my.patch, and then did patch -p1 < my.patch on my tree. I got better results, some conflicts, but a much cleaner result.

What does git does special with cherry-picks? I use git 1.7.0.4.

Edited: By cleaner results, I mean the resulting tree matched a lot more the results of git show X, whereas the git cherry-pick included a lot more code.

0x6adb015
  • 6,479
  • 3
  • 23
  • 36
  • 1
    It would be immensely useful to create a reduced testcase. – Josh Lee Mar 01 '11 at 16:37
  • I'm with jleedev here: your description of the discrepancy is still really hard to understand. `git show` shows you the diff, while your tree is the full contents. It makes more sense to compare `git show X` and `git show X'`, where `X'` is the cherry-picked version. Does `X'` contain much more code in its diff? – Cascabel Mar 02 '11 at 15:34

2 Answers2

9

When you cherry-pick a commit, it commits the result using all the metadata of the commit, not just the diff it represents - you'll get the original commit message and author. Your patch pipeline will get you the working tree contents that you want, but then you'll have to commit it yourself, hopefully with git commit -c <original-commit> to copy the metadata like cherry-pick would have. Cherry-pick also has some additional options that could be helpful, and can accept multiple commits (perhaps specified as a rev-list range). patch obviously doesn't support any of that.

I'm not sure about your assertion that the result was "cleaner". Are you suggesting that git applied the diff differently than patch did?

Cascabel
  • 422,485
  • 65
  • 357
  • 307
  • 1
    Yes. The in-tree results I got were very different using git-cherry-pick and patch. I am trying to understand why. – 0x6adb015 Mar 01 '11 at 16:23
  • @0x6adb015: Well, I can't say much more than that git uses its own diff/patch machinery internally, and it's obviously not identical to GNU diff and patch. I've seen differences, but mostly git doing better when patch doesn't have as much information as git has access to - not generally patch performing better. If you think git's result was *wrong*, you might want to report it as a bug. – Cascabel Mar 01 '11 at 16:25
1

This might help:

http://technosophos.com/2009/12/04/git-cherry-picking-move-small-code-patches-across-branches.html

It might be partly off topic, but as you can see, cherry picking seems to track code blocks across the code in some way, much more advanced than what I would guess patch does, which is probably just parsing the two codebases sequentially and side by side, and mark lines that differ.

Ryne Everett
  • 5,215
  • 3
  • 32
  • 42
Rolf
  • 111
  • 2