git version 2.19.0
I have two commits for file 'myfile' I am cherry picking from one branch to another branch. During the last patch apply, I get "error: mydir/myfile: does not match index".
1) I generate the two patch files commit1.patch (oldest) and commit2.patch (newest):
cd /target-branch
git --git-dir=/source-branch/myrepo/.git format-patch -k -1 <commit N's ID>
2) I apply patch #1:
git apply -3 commit1.patch
error: patch failed: mydir/myfile:17
error: repository lacks the necessary blob to fall back on 3-way merge.
error: mydir/myfile: patch does not apply
...which fails as expected because two of the hunks in patch modify code blocks added in a commit older than commit #1. I'm not applying that older commit because it's a fix I do not want in target branch.
3) I reapply patch 1 using --reject instead of -3 to skip applying the two non-applicable hunks:
git apply --reject 1.patch
... which logs no errors and logs the two patch hunks it skipped as expected, and applies the rest of the patch.
4) I apply patch #2:
git apply -3 commit2.patch
error: mydir/myfile: does not match index
Why error "does not match index"? 'git status' shows staging is empty, 'git fsck' reports no errors, 'git diff --cached' reports nothing.
I found a workaround: Fresh clone the target branch, edit-out the two hunks which cannot be applied from commit1.patch, then do:
git apply -3 commit1.patch
git apply -3 commit2.patch
… which works with no errors
How can I resolve the "does not match index" error commit2.patch outputs, other than first manually removing the two hunks from commit1.patch?