Skip to content

Commit ede3d80

Browse files
committed
rn-129: add article about cherry-pick vs apply
1 parent 81a89bd commit ede3d80

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

rev_news/drafts/edition-129.md

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,53 @@ This edition covers what happened during the months of October and November 2025
2525
### Reviews
2626
-->
2727

28-
<!---
2928
### Support
30-
-->
29+
30+
+ [[Bug report] git cherry-pick silently ignores error whereas git apply fails for hunk apply](https://lore.kernel.org/git/CAEyHQXWd77_jJachC6FYbWMJ+L=KkKoUqiACQ7z8r-ZwYq8JYw@mail.gmail.com/)
31+
32+
Bhavik Bavishi filed and sent a bug report to the mailing
33+
list. Running `git cherry-pick` failed to apply some changes but
34+
didn't report any error. On the contrrary when creating a patch
35+
using `git format-patch` from the same commit and applying it using
36+
`git apply --verbose`, the latter command also failed to apply the
37+
same changes but errored out. It seemed that there shouldn't be such
38+
a behavior discrepancy and that `git cherry-pick` should have
39+
reported an error too.
40+
41+
Johannes Sixt, suggested using `git apply --3way` to apply the
42+
patch. He was interested not only on the success or failure of the
43+
command but also on the end result of applying the patch. Was that
44+
end result similar as the result from `git cherry-pick` or
45+
different?
46+
47+
Bhavik reported back that indeed `git apply --3way` succeeded and
48+
produced the same end result as `git cherry-pick`. Even if it looked
49+
like `git cherry-pick` worked as expected, that still seemed a
50+
strange behavior though.
51+
52+
Johannes Sixt replied that a merge strategy is used by both
53+
`git cherry-pick` and `git apply --3way`. Unlike a simple patch
54+
application, a merge strategy is intelligent enough to detect if a
55+
change has already been applied. He illustrated this with an example
56+
where text repeats in a file, but only specific instances are
57+
modified.
58+
59+
In the meantime, Chris Torek also replied to Bhavik providing a
60+
wealth of explanations. He explained that `git apply` works with a
61+
*patch*, which is essentially a "we expect the file looks like this"
62+
instruction. If the file doesn't match the expected context lines
63+
exactly, the patch fails.
64+
65+
In contrast, `git cherry-pick` performs a *3-way merge*. It locates
66+
a "common base version" (the ancestor), compares it to "Ours"
67+
(current branch), and "Theirs" (the commit being picked) . If the
68+
merge logic sees that "Theirs" introduces a change that "Ours" has
69+
already made, it silently discards the duplicate change rather than
70+
erroring out. This confirms that the command was working as
71+
intended, using the full history to resolve what looked like a
72+
conflict to the simpler `git apply` tool.
73+
74+
Bhavik thanked Chris for the helpful explanations.
3175

3276
<!---
3377
## Developer Spotlight:

0 commit comments

Comments
 (0)