【问题标题】:git create patch with diffgit 创建带差异的补丁
【发布时间】:2013-04-06 07:24:59
【问题描述】:

我试过了

git diff 13.1_dev sale_edit > patch.diff

然后我尝试在另一个分支中执行git apply patch.diff,但我得到的补丁不适用。如何从可以与 git apply 一起使用的差异创建补丁文件?

收到的错误:

$ git apply --ignore-space-change --ignore-whitespace diff.diff 
diff.diff:9: trailing whitespace.

diff.diff:10: trailing whitespace.
    function set_change_sale_date() 
diff.diff:12: space before tab in indent.
      $this->sale_lib->set_change_sale_date($this->input->post('change_sale_date'));
diff.diff:14: trailing whitespace.

diff.diff:15: trailing whitespace.
    function set_change_sale_date_enable() 
warning: application/controllers/sales.php has type 100755, expected 100644
error: patch failed: application/controllers/sales.php:520
error: application/controllers/sales.php: patch does not apply
warning: application/language/english/sales_lang.php has type 100755, expected 100644
error: patch failed: application/language/english/sales_lang.php:134
error: application/language/english/sales_lang.php: patch does not apply
warning: application/libraries/Sale_lib.php has type 100755, expected 100644
error: patch failed: application/models/sale.php:170
error: application/models/sale.php: patch does not apply
warning: application/views/sales/register.php has type 100755, expected 100644
error: patch failed: application/views/sales/register.php:361
error: application/views/sales/register.php: patch does not apply

我在 Mac 上试试这个

【问题讨论】:

  • Git 不适合这项工作。您可能会花费数小时尝试应用补丁,因为 Git 无法正确处理空格和行尾。我曾经花了一个多小时尝试应用通过电子邮件发送给我的三行补丁(另见Apply patch to file that's under Git without using Git?)。使用patch 程序。它可以处理空格和行尾,而不会到处乱扔垃圾。

标签: git patch


【解决方案1】:

对于 git 版本 1.9.1,我在使用“git apply”应用使用“git diff”创建的补丁时看到了类似的抱怨。

似乎 1.9.1 git 在处理补丁文件中的空格和制表符混合时遇到问题。

warning: squelched 1 whitespace error warning: 6 lines add whitespace errors.

@VonC 的回答没有帮助,我仍然收到同样的警告。

最简单的解决方案是简单地使用“patch”命令,该命令成功地将“git diff”输出中捕获的所有更改应用到目标 git 目录。

$ patch --version GNU patch 2.7.1

【讨论】:

    【解决方案2】:

    您可以将补丁应用为 3 路合并:

    git diff 13.1_dev sale_edit > patch.diff
    git apply -3 patch.diff
    

    它应该会引发冲突,以便您可以手动解决。 或者您可以使用单线,将补丁直接通过管道传递到 git-apply:

    git diff 13.1_dev sale_edit | git apply -3
    

    要反转补丁:

    git diff 13.1_dev sale_edit | git apply -3 -R
    

    (注意:这与上面的命令相同,没有创建补丁文件的两阶段过程)

    git help apply
    
    -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...
    

    【讨论】:

      【解决方案3】:

      这里你必须用你有差异的分支来试试。

      git diff 13.1_dev sale_edit > patch.diff yourBranch()
      

      【讨论】:

        【解决方案4】:

        试一试:

        git apply --ignore-space-change --ignore-whitespace patch.diff
        

        如“git: patch does not apply”中所述,这可能是由以下原因引起的:

        • 本地文件系统和远程存储库之间的行尾不同。
          .gitattributes 文件中的用户 core.eol 是一个不错的方法(参见“git force file encoding on commit”)
        • 执行位 ('x')。
          这可能会导致您设置git config core.filemode false,然后设置git reset --hard HEAD(确保您没有未提交的更改,否则它们会丢失)。

        【讨论】:

        • 你能提供更多关于执行位的信息吗?
        • @ChrisMuench 你能在git config core.filemode false之后尝试同样的命令吗?
        • 仍然给我模式错误。我什至尝试通过 --global
        • @ChrisMuench 会 stackoverflow.com/questions/4770177/… 帮忙吗? (git reset --hard HEAD 以应用新选项)
        • @ChrisMuench 所以重置后 git apply 仍然失败?你正在使用哪个操作系统和 git 版本?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-26
        • 1970-01-01
        • 2011-09-30
        • 1970-01-01
        • 2022-11-12
        • 2020-08-13
        相关资源
        最近更新 更多