如何解决 git am 的 "patch does not apply" 错误,类似于合并冲突?

5
我想将一些提交从一个仓库移动到另一个仓库,并且我希望保留原始的提交、日期和提交消息,而不是将整个内容压缩在一起。git format-patch master可以做到这点,为每个提交制作一个.patch文件,但我在应用它们时遇到了一些问题(我卡在了第一个提交上)。git apply 可以干净地应用更改,没有冲突或其他问题,但当然它不会创建我想要的提交。另一方面,git am --committer-date-is-author-date -3 < 失败了,没有明确的解决方法。
Applying: <original commit message>
error: patch failed: war/WEB-INF/web.xml:162
error: war/WEB-INF/web.xml: patch does not apply
<more files listed, all of them ones that I've modified in the commit; newly created files are not listed>
error: Did you hand edit your patch?
It does not apply to blobs recorded in its index.
hint: Use 'git am --show-current-patch' to see the failed patch
Using index info to reconstruct a base tree...
M       war/WEB-INF/web.xml
Patch failed at 0001 <commit message>
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".

运行完这个命令后,我的工作副本或暂存区没有任何更改。我本来期望会得到带有合并冲突标记的更改文件(如<<<<<<<<>>>>>>>>),因为我使用了-3选项,但实际上我连那些都没有。是否有一种方法可以运行git am以获得我想要的结果,或者我应该使用git apply并创建新的提交?

1
尝试使用 --reject。冲突的块将保存在 .rej 文件中。 - ElpieKay
感谢@ElpieKay! 冲突解决有点不同,但它让我得到了我想要的 - 它让我保留了原始提交消息、日期和内容。如果你将其作为答案发布,我会接受它。 - kjerins
1个回答

3
你可以尝试使用 --reject。它将应用可适用的补丁部分,并将被拒绝的巨块留在相应的 *.rej 文件中。当 -3 不起作用时,它会提供帮助。

3
谢谢。这很有帮助,但我真的希望有一个选项只添加合并冲突标记而不创建.rej文件。 - ADTC
2
使用以下命令将.rej文件转换为合并冲突标记:find . -iname '*.rej' -exec basename {} .rej ';' | xargs -I _ wiggle --replace _ _.rej - Daniel T

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接