【问题标题】:Error while applying a patch in git在 git 中应用补丁时出错
【发布时间】:2012-10-22 20:14:31
【问题描述】:

我有一个浅克隆,我在上面做了三个提交。 这是日志:

$ git log --oneline --graph --decorate --all
* d3456fd (HEAD, master) 补丁 3
* 9713822 补丁 2
* 6f380a6 补丁 1
* 来自完整克隆的 8a1ce1e (origin/master, origin/HEAD)
* 7c13416 从浅层添加
* 3b3ed39 已删除电子邮件
* cfbed6c 进一步修改
* 为 2.1 版添加了 a71254b
* 第 2 版的 7347896(嫁接)更改

现在我从这里创建一个补丁:

$ git format-patch -k --stdout origin > ../format_since_origin.patch

我想将此补丁应用到另一个克隆中,这是一个完整的克隆。
这是日志:

$ git log --oneline --graph --decorate --all
* 8a1ce1e (HEAD, origin/master, master) 来自完整克隆
* 7c13416 从浅层添加
* 3b3ed39 已删除电子邮件
* cfbed6c 进一步修改
* 为 2.1 版添加了 a71254b
* 第 2 版的 7347896 处更改
* b1a8797 对 ttwo 文件的更改
* 603710c 更改测试报告
* 16b20b3 添加了 test_report.txt
* f0871ea 修改了 file1.xml
* dd94bfc 添加了 file1.xml
* 00758aa 第二次提交
* 49f9968 第一次提交

我无法应用从上面的浅克隆创建的补丁。我收到以下错误。

$ git am -3 /c/temp/git/format_since_origin.patch
应用:补丁 1
使用索引信息重建基础树...
错误:补丁失败:file1.c:6
错误:file1.c:补丁不适用
您是否手动编辑了您的补丁?
它不适用于记录在其索引中的 blob。
无法回退到三向合并。
补丁在 0001 补丁 1 失败
解决此问题后,运行“git am --resolved”。
如果您希望跳过此补丁,请运行“git am --skip”。
要恢复原始分支并停止修补,请运行“git am --abort”。

知道为什么这个补丁失败了吗?还是我的方法完全错误?

更新:

它适用于以下

$ git am -3 --ignore-whitespace /c/temp/git/format_since_origin.patch 应用:补丁 1 应用:补丁 2 应用:补丁 3

现在,正如 Charles 所建议的那样 - 如果我尝试 git diff,我会收到如下错误。

$ git diff -p origin > ../dif_origin.patch

申请时,

$ git apply --ignore-whitespace --inaccurate-eof /c/temp/git/dif_origin.patch
c:/temp/git/dif_origin.patch:9:尾随空格。
file1.c 的补丁更改
c:/temp/git/dif_origin.patch:18:尾随空格。
修补这个 xml 家伙
c:/temp/git/dif_origin.patch:29:尾随空格。
fsdfsd
c:/temp/git/dif_origin.patch:30:尾随空格。
修补此报告
错误:补丁失败:file1.c:6
错误:file1.c:补丁不适用
错误:补丁失败:file1.xml:2
错误:file1.xml:补丁不适用
错误:补丁失败:tr/test_report.txt:2
错误:tr/test_report.txt:补丁不适用

【问题讨论】:

  • 有理由不使用git bundle甚至git diff/git apply吗?
  • @charles,我在使用 git diff 和应用时遇到错误。我已在原始帖子中添加了错误详细信息。我创建的差异是错误的吗?

标签: git


【解决方案1】:

请注意,必须忽略空格的一个理由是 (June 2010):

它的作用是启用GMail -> download -> git-am workflow
GMail(当然还有无数其他的)电子邮件提供商在原始电子邮件消息的开头引入了空格,而在其他情况下则保持原样。

如“git am/format-patch: control format of line endings”中提到的,你可以试试:

 git am --keep-cr

这不需要您忽略空格(仅警告)。

OP maxmelbin 确认 in the comments 以下是有效的:

 git am -3 --keep-cr --committer-date-is-author-date /c/temp/git/format_since_origin.patch

【讨论】:

    【解决方案2】:

    当 git apply 正常工作时,你根本没有输出:

    $ git apply example.patch
    [nothing returned]
    

    如果您想查看幕后发生的事情,可以使用 -v(详细)标志:

    $ git apply -v example.patch
    Checking patch includes/common.inc...
    Applied patch includes/common.inc cleanly.
    

    但是,如果从您自己的本地 git 工作副本中运行 git apply,即使使用 -v(详细)标志,git apply 也可能什么都不做,并且不提供任何输出。如果发生这种情况,请检查您在目录树中的位置 - git apply 可能在其他位置工作。

    git apply 的替代方法是使用 patch 命令:

    $ patch -p1 < example.patch
    

    这是 git apply 命令可以生成的其他输出,以及它的含义。 补丁不适用

    $ git apply example.patch
    error: patch failed: includes/common.inc:626
    error: includes/common.inc: patch does not apply``
    

    Git 无法应用补丁中的更改,因为它无法找到有问题的代码行;它们必须已被另一个提交更改或删除。试试这些:

    确保尚未应用补丁。在 git-log 中查找它或简单地检查代码以查看更改是否已经存在。如果他们是,你就完成了。如果不是,或者只有其中一些是,请尝试其他方法:

    使用patch -p1 &lt; filename.patch。虽然 git-apply 完全拒绝有任何错误的补丁,但 patch -p1 会逐个工作,尽可能多地应用单独的更改。它在修改之前将每个文件备份为 filename.ext.orig,并将被拒绝的大块保存在 filename.ext.rej 中。丢弃 .orig 文件并手动应用 .rej 中留下的更改。对于小补丁来说,这是一个简单的策略。

    【讨论】:

    • patch -p1 &lt; filename.patch 拯救了我的一天!谢谢!
    【解决方案3】:

    好的。以下工作。

    $ git am -3 --ignore-whitespace /c/temp/git/format_since_origin.patch
    应用:补丁 1
    应用:补丁 2
    应用:补丁 3

    【讨论】:

    • 根据 VonC 的回复,以下内容也将生成相同的 SHA1 - git am -3 --keep-cr --committer-date-is-author-date /c/temp/git/format_since_origin .patch
    猜你喜欢
    • 1970-01-01
    • 2015-05-21
    • 2017-07-12
    • 2022-09-29
    • 2018-06-29
    • 2014-11-08
    • 2020-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多