git用diff创build补丁

我试过了

git diff 13.1_dev sale_edit > patch.diff 

然后,我尝试在另一个分支做git apply patch.diff ,但是我得到的补丁不适用。 如何创build补丁文件,我可以使用git应用?

收到的错误:

 $ 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 apply --ignore-space-change --ignore-whitespace patch.diff 

正如在“ git:补丁不适用 ”中提到的,这可能是由于:

  • 本地文件系统和远程回购之间的行结尾是不同的。
    .gitattributes文件中的用户core.eol是一个很好的方法(请参阅“ 提交时的git强制文件编码 ”)
  • 执行位(' x ')。
    这可能会导致您将git config core.filemode false设置git config core.filemode false ,然后执行git reset --hard HEAD (确保您没有未提交的更改,否则将会丢失)。

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

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

它应该引起冲突,以便您可以手动解决。 或者你可以用一行代码,直接将补丁pipe道粘贴到git上:

 git diff 13.1_dev sale_edit | git apply -3 

扭转补丁:

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

(注意:这和上面的命令是一样的,没有两步创build补丁文件的过程)

 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... 

在这里,你必须尝试与你有差异的分支。

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