git用diff创建补丁

时间:2013-04-13 22:58:30

标签: git patch

我试过

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上尝试这个

4 个答案:

答案 0 :(得分:25)

尝试:

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

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

  • 本地文件系统和远程仓库之间的行结尾不同。
    core.eol文件中的用户.gitattributes是一种很好的方法(请参阅“git force file encoding on commit”)
  • 执行位('x') 这可以让您设置git config core.filemode false,然后设置git reset --hard HEAD(确保您没有未提交的更改,否则会丢失)。

答案 1 :(得分:16)

您可以将补丁应用为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...

答案 2 :(得分:1)

在这里你必须尝试使用​​你有差异的分支。

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

答案 3 :(得分:1)

使用git版本1.9.1,我在使用“git apply'应用使用' git diff'。

创建的补丁

似乎1.9.1 git处理空间混合的问题&补丁文件中的选项卡。

$ patch --version GNU patch 2.7.1

@ VonC的答案没有用,我仍然收到相同的警告。

最简单的解决方案是简单地使用' 补丁'成功应用' git diff'中捕获的所有更改的命令输出到目标git目录。

{{1}}