What are the difference between Cherry-pick and patch apply?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 17:54:58

问题


I am aware about git cherry-pick and git apply terminalogies. But today i came across with some issue. I was applying one patch by using the diff of two revisions. I used the below command:

git diff HEAD > diff.patch

git apply diff.patch

I got patch apply failed for one file.

Then i simply tried to cherry-pick the commit-id2. It is cherry-picked successfully.

What may be the reason? Anyone wants to throw some light on the same.


回答1:


Two possibilities:

  • The changes in diff.patch are probably different from the changes in commit-id2, which is why the two behaved differently.

    git diff HEAD shows the diff between HEAD and the current working directory. git cherry-pick commit-id2 applies the diff between commit-id2 and its parent (and commits the result using the metadata from commit-id2). The diff between commit-id2 and its parent is not (usually) the same as the diff between HEAD and the current working directory.

  • You did not tell git apply to fall back to a 3-way merge like cherry-pick does. Try adding the --3way (or -3) option to git apply.

Try the following:

git diff commit-id2^! >diff.patch
git apply -3 diff.patch

The above should behave like git cherry-pick -n commit-id2. (See git help revisions for an explanation of the ^! syntax.)



来源:https://stackoverflow.com/questions/30198013/what-are-the-difference-between-cherry-pick-and-patch-apply

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!