Search code examples
gitgit-cherry-pick

How to raise a conflict for this git cherry-pick?


Two branches master and foo point to the same root commit A. The work tree has only one file bar.txt.

$cat bar.txt
11111
22222
33333
44444
55555

Make Commit B to master, adding a line in bar.txt.

$cat bar.txt
11111
22222
33333
aaaaa
44444
55555

Then make Commit C to master, deleting aaaaa and adding bbbbb elsewhere in bar.txt.

$cat bar.txt
11111
bbbbb
22222
33333
44444
55555

Run git format-patch -1 C to generate the patch for Commit C, assuming its name to be 0001-bbbbb.patch.

Now checkout foo. git am 00001-bbbbb.patch or git apply 00001-bbbbb.patch will fail due to conflicts, as expected. However git cherry-pick C will succeed without any problem.

From this question I learn that git am or git apply will succeed too with the argument -3 or --3way in this case. However I cannot find any config or argument about three way merge for git cherry-pick.

Question:

How to disable the three-way-merge thing for git cherry-pick in this case, so that the cherry-pick will fail?

Thanks a lot.


Solution

  • You can specify the merge strategy when using git cherry-pick. The strategies available are listed here: https://git-scm.com/docs/merge-strategies.

    EDIT: octopus is not actually the correct merge strategy in this case since it also fails with a valid cherry-pick (as @Elpiekay pointed out)

    I'm not sure any of the other valid cherry-pick merge strategies (and options) will result in the same behavior as the git apply above. I'd be happy to be proven wrong though!

    I think you probably want octopus, so your command would be:

    git cherry-pick --strategy=octopus C

    When I tested it, it gave me this error:

    error: could not apply 62f20c5... C