我是错误 : "patch does not apply"

标签 git

我正在尝试使用 git 将多个提交从一个项目移动到第二个类似的项目。

所以我创建了一个补丁,包含 5 个提交:

git format-patch 4af51 --stdout > changes.patch

然后将补丁移动到第二个项目的文件夹并想要应用补丁:

git am changes.patch 

...但它给了我错误:

Applying: Fixed products ordering in order summary.
error: patch failed: index.php:17
error: index.php: patch does not apply
Patch failed at 0001 Fixed products ordering in order summary.
The copy of the patch that failed is found in:
   c:/.../project2/.git/rebase-apply/patch
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".

所以我打开了 index.php,但那里没有任何改变。我假设有一些 >>>>> 标记等,比如在解决 merge 冲突时,但文件中没有标记冲突。 git status 也给了我已更改文件的空列表(只有 changes.patch 在那里)。所以我运行 git am --continue,但出现另一个错误:

Applying: Fixed products ordering in order summary.
No changes - did you forget to use 'git add'?
If there is nothing left to stage, chances are that something else
already introduced the same changes; you might want to skip this patch.
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". 

我正在使用 Windows 7 和最新的 git 版本“1.9.4.msysgit.1”

附言经过几个小时的谷歌搜索,我发现了一些解决方案,但对我来说没有任何用处:


git am -3 changes.patch 

给出奇怪的“sha1 信息”错误:

Applying: Fixed products ordering in order summary.
fatal: sha1 information is lacking or useless (index.php).
Repository lacks necessary blobs to fall back on 3-way merge.
Cannot fall back to three-way merge.
Patch failed at 0001 Fixed products ordering in order summary.
The copy of the patch that failed is found in:
   c:/.../project2/.git/rebase-apply/patch
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". 

git am changes.patch --ignore-whitespace --no-scissors --ignore-space-change

给出第一个错误,如上所示:“错误:补丁失败:index.php:17”,但 index.php 中没有添加冲突标记。

最佳答案

什么是补丁?

补丁只不过是一系列指令(见下文):“在此处添加”、“在此处删除”、“将第三项更改为第四项”。这就是 为什么 Git 告诉你:

The copy of the patch that failed is found in:
c:/.../project2/.git/rebase-apply/patch

您可以在您喜欢的查看器或编辑器中打开该补丁,在您喜欢的编辑器中打开要更改的文件,然后使用您知道的(和当要更改的文件现在看起来很少或根本不像他们之前更改时所做的那样时,Git 不会弄清楚如何“在此处添加”更改作为补丁提供给您。

多一点

三向 merge 引入了比简单的“指令系列”“多一点”的信息:它还告诉您文件的原始 版本是什么。如果您的存储库具有原始版本,则在您的存储库中运行的 Git 软件可以将 对文件所做的操作与补丁 对文件所做的操作进行比较.

正如您在上面看到的,如果您请求三向 merge ,Git 无法在其他存储库中找到“原始版本”,因此它甚至无法尝试三向 merge merge 。因此,您不会得到任何冲突标记,并且您必须手动执行补丁应用。

(有些情况下会缺少“多一点”部分。额外的信息由补丁中的 Index: 行提供,例如:

diff --git a/go.mod b/go.mod
index 1fefa60..38a3a41 100644

第二行可以这样写,例如:

index 1fefa6021dcd205c1243e236d686595920d9621b..38a3a41434fda3a68ce3356092a89afca81eb614 100644

在更完整的案例中。请注意两个哈希 ID,由两个点分隔。左边的那个是您的 Git 软件将使用的那个,用于尝试在您自己的存储库中查找具有给定哈希 ID 的文件。将提供的补丁应用于该文件,该文件必须存在必须工作并且必须生成一个哈希 ID 为右侧的文件,并且完成所有这些操作后 Git所需的所有其他信息。)

使用--reject

当您必须手动应用补丁时,Git 仍然有可能自动为您应用大部分 补丁,只留下少数部分给具有推理能力的实体代码(或任何需要修补的东西)。添加 --reject 告诉 Git 这样做,并将补丁的“不适用”部分留在拒绝文件中。 如果您使用此选项,您仍然必须手动应用每个失败的补丁,并弄清楚如何处理被拒绝的部分。

完成所需的更改后,您可以git add 修改后的文件并使用git am --continue 告诉Git 提交更改并继续下一个补丁。

如果无事可做怎么办?

由于我们没有您的代码,所以我无法判断是否是这种情况,但有时,您最终会得到其中一个补丁说的内容相当于,例如,“修复某个单词的拼写第 42 行”,那里的拼写已经固定。

在这种特殊情况下,您在查看补丁和当前代码后,应该对自己说:“啊哈,这个 补丁应该完全跳过!”那是你使用 Git 已经打印的其他建议的时候:

If you prefer to skip this patch, run "git am --skip" instead.

如果你运行 git am --skip,Git 将跳过那个补丁,这样如果邮箱中有五个补丁,它最终只会添加四个提交,而不是五个(如果你跳过两次,则为三个而不是五个,依此类推)。

关于我是错误 : "patch does not apply",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25846189/

相关文章:

git - 提交前显示 git 的压缩提交大小

php - Git 在 merge 分支时混合两个函数

git - 如何创建 GitLab webhook?

git - 如何处理导致失败的外部代码的git bisect

windows - Git Bash 控制台在打开时崩溃

git - 如何删除所有 Gitlab 存储库?

git - 当我希望我的分支总是更新时使用 Git

git - 我如何做一个什么都不做的 git merge?

git - 如何 git 只列出被跟踪的目录?

java - TeamCity 8.x 中特定分支的 Git 提交的版本号