git 创建带有差异的补丁
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15993861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
git create patch with diff
提问by Chris Muench
I tried doing
我试着做
git diff 13.1_dev sale_edit > patch.diff
Then I tried doing git apply patch.diff
in another branch, but I got patch does not apply. How do I create patch files from diffs that I can use with git apply?
然后我尝试git apply patch.diff
在另一个分支中做,但我得到的补丁不适用。如何从可以与 git apply 一起使用的差异创建补丁文件?
Errors received:
收到的错误:
$ 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
I'm trying this on Mac
我正在 Mac 上尝试这个
回答by VonC
Try a:
尝试一个:
git apply --ignore-space-change --ignore-whitespace patch.diff
As mentioned in "git: patch does not apply", this can be caused by:
如“ git: patch does not apply”中所述,这可能是由以下原因引起的:
- the line endings differing between the local file system and the remote repo.
Usercore.eol
in.gitattributes
file is a good approach (see "git force file encoding on commit") - the execution bit ('
x
').
That can lead you to setgit config core.filemode false
, followed by agit reset --hard HEAD
(make sure you don't have uncommitted changes, or they would be lost).
- 本地文件系统和远程仓库之间的行尾不同。
用户core.eol
在.gitattributes
文件是一个好方法(见“混帐力编码的文件在提交”) - 执行位 ('
x
')。
这可能会导致您设置git config core.filemode false
,然后是一个git reset --hard HEAD
(确保您没有未提交的更改,否则它们会丢失)。
回答by Jeff Kiiza
You can apply the patch as a 3-way merge:
您可以将补丁应用为 3 路合并:
git diff 13.1_dev sale_edit > patch.diff
git apply -3 patch.diff
It should bring up the conflict so that you can resolve manually. Or you could go with a one-liner, piping the patch to git-apply directly:
它应该会引发冲突,以便您可以手动解决。或者,您可以使用单衬,将补丁直接通过管道输送到 git-apply:
git diff 13.1_dev sale_edit | git apply -3
To reverse the patch:
要反转补丁:
git diff 13.1_dev sale_edit | git apply -3 -R
(note: this is same as the commands above, without the two-stage process of creating the patch file)
(注意:这和上面的命令一样,没有创建补丁文件的两阶段过程)
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...
回答by Tzunghsing David Wong
With git version 1.9.1, I am seeing similar complaints when use 'git apply' to apply the patch created using 'git diff'.
对于 git 1.9.1 版,当使用 'git apply' 应用使用 'git diff' 创建的补丁时,我看到了类似的抱怨。
It seems 1.9.1 git is having problem dealing with mixture of spaces & tabs in the patch file.
似乎 1.9.1 git 在处理补丁文件中的空格和制表符混合时遇到问题。
warning: squelched 1 whitespace error
warning: 6 lines add whitespace errors.
warning: squelched 1 whitespace error
warning: 6 lines add whitespace errors.
@VonC's answer does not help and I am still getting the same warnings.
@VonC 的回答没有帮助,我仍然收到相同的警告。
The easiest solution is to simply use the 'patch' command which successfully applies all changes captured in 'git diff' output to the target git directory.
最简单的解决方案是简单地使用“补丁”命令,该命令成功地将“git diff”输出中捕获的所有更改应用到目标 git 目录。
$ patch --version
GNU patch 2.7.1
$ patch --version
GNU patch 2.7.1
回答by User123456
Here you have to try it with the branch you have diff with.
在这里你必须用你有差异的分支来尝试它。
git diff 13.1_dev sale_edit > patch.diff yourBranch()