简体   繁体   English

当先前的应用跳过块时,“ git apply”给出错误“与索引不匹配”

[英]“git apply” gives error “does not match index” when a previous apply skips hunks

git version 2.19.0 git版本2.19.0

I have two commits for file 'myfile' I am cherry picking from one branch to another branch. 我对文件'myfile'进行了两次提交,这是我从一个分支到另一个分支的选择。 During the last patch apply, I get "error: mydir/myfile: does not match index". 在上一个补丁应用期间,出现“错误:mydir / myfile:与索引不匹配”。

1) I generate the two patch files commit1.patch (oldest) and commit2.patch (newest): 1)我生成两个补丁文件commit1.patch(最旧)和commit2.patch(最新):

cd /target-branch  
git --git-dir=/source-branch/myrepo/.git format-patch -k -1 <commit N's ID>  

2) I apply patch #1: 2)我应用补丁#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. ...之所以失败,是因为补丁程序中的两个块修改了比提交#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: 3)我使用--reject而不是-3重新应用补丁1,以跳过应用两个不适用的块:

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: 4)我应用补丁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. 'git status'显示暂存为空,'git fsck'不报告错误,'git diff --cached'不报告任何内容。

I found a workaround: Fresh clone the target branch, edit-out the two hunks which cannot be applied from commit1.patch, then do: 我找到了一种解决方法:重新克隆目标分支,编辑掉无法从commit1.patch应用的两个块,然后执行以下操作:

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? 除了首先手动从commit1.patch中删除两个块以外,如何解决“不匹配索引”错误commit2.patch输出?

The answer to your issue is in the git apply documentation , but in several parts, to which I added boldface (the options were already boldfaced): 您问题的答案在git apply文档中 ,但在几个部分中,我添加了黑体字(选项已经用黑体字显示):

-3, --3way -3,--3way
When the patch does not apply cleanly, fall back on 3-way merge if the patch records the identity of blobs it is supposed to apply to, and we have those blobs available locally, possibly leaving the conflict markers in the files in the working tree for the user to resolve. 如果补丁不能完全适用,则如果该补丁记录了它应该应用于的Blob的标识,则退回到三路合并,并且这些Blob在本地可用,可能会将冲突标记保留在工作树的文件中供用户解决。 This option implies the --index option , and is incompatible with the --reject and the --cached options. 此选项暗含--index选项 ,并且与--reject--cached选项不兼容。

From here you must back up a bit: 从这里您必须备份一点:

--index - 指数
When --check is in effect, or when applying the patch (which is the default when none of the options that disables it is in effect), make sure the patch is applicable to what the current index file records. --check生效时,或在应用补丁程序时(这是所有禁用它的选项都没有生效的默认设置),请确保该补丁程序适用于当前索引文件记录的内容。 If the file to be patched in the working tree is not up to date, it is flagged as an error. 如果要在工作树中修补的文件不是最新的,则将其标记为错误。 This flag also causes the index file to be updated. 此标志还导致索引文件被更新。

So by using -3 you turned on --index , and --index explicitly calls for an error if the work-tree copy of the file does not match the index copy of the file. 因此,如果文件的工作树副本与文件的索引副本不匹配,则使用-3可以打开--index ,并且--index显式调用错误。

Earlier, in step 3, you manually ran git apply --reject , without -3 or --index . 之前,在第3步中,您手动运行git apply --reject没有 -3--index That doesn't fail when creating the .rej file, but does leave the index and work-tree copies different. 在创建时不会失败.rej文件,但留下的指数和工作树副本不同。

If there is to be no point in using -3 , simply leave it out in both cases, so that you apply without updating the index copy of the file and without constraining the work-tree copy of the file to match the index copy of the file either. 如果没有必要使用-3 ,则在两种情况下都将其省略,这样就可以在不更新文件的索引副本且不限制文件的工作树副本以匹配文件的索引副本的情况下进行应用。归档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM