在 git 中应用补丁时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13190679/
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
Error while applying a patch in git
提问by maxmelbin
I have a shallow clone, on which i made three commits.
Here is the log:
我有一个浅克隆,我在上面做了三个提交。这是日志:
$ git log --oneline --graph --decorate --all
* d3456fd (HEAD, master) patch 3
* 9713822 patch 2
* 6f380a6 patch 1
* 8a1ce1e (origin/master, origin/HEAD) from full clone
* 7c13416 added from shallow
* 3b3ed39 removed email
* cfbed6c further modifications
* a71254b added for release 2.1
* 7347896 (grafted) changes for release 2
$ git log --oneline --graph --decorate --all
* d3456fd (HEAD, master) patch 3
* 9713822 patch 2
* 6f380a6 patch 1
* 8a1ce1e (origin/master, origin/HEAD) from full clone
* 7c13416 添加自浅
* 3b3ed39 删除了电子邮件
* cfbed6c 进一步修改
* 为 2.1 版添加了a71254b * 为 2 版添加了
7347896(嫁接)更改
now i create a patch from here:
现在我从这里创建一个补丁:
$ git format-patch -k --stdout origin > ../format_since_origin.patch
$ git format-patch -k --stdout origin > ../format_since_origin.patch
I want to apply this patch in another clone, which is a full clone.
Here is the log:
我想在另一个克隆中应用这个补丁,这是一个完整的克隆。
这是日志:
$ git log --oneline --graph --decorate --all
* 8a1ce1e (HEAD, origin/master, master) from full clone
* 7c13416 added from shallow
* 3b3ed39 removed email
* cfbed6c further modifications
* a71254b added for release 2.1
* 7347896 changes for release 2
* b1a8797 changes to ttwo files
* 603710c changed test report
* 16b20b3 added test_report.txt
* f0871ea modified file1.xml
* dd94bfc added file1.xml
* 00758aa second commit
* 49f9968 first commit
$ git log --oneline --graph --decorate --all
* 8a1ce1e (HEAD, origin/master, master) 来自完整克隆
* 7c13416 添加自浅
* 3b3ed39 删除电子邮件
* cfbed6c 进一步修改
* a71254b 添加用于 2.1
* 7347896版本 2 的
更改 * b1a8797 对 ttwo 文件的更改
* 603710c 更改了测试报告
* 16b20b3 添加了 test_report.txt
* f0871ea 修改了 file1.xml
* dd94bfc 添加了 file1.xml
* 00758aa 第二次提交
* 49f9968 第一次提交
I am unable to apply the patch created from the shallow clone above. I get the following error.
我无法应用从上面的浅层克隆创建的补丁。我收到以下错误。
$ git am -3 /c/temp/git/format_since_origin.patch
Applying: patch 1
Using index info to reconstruct a base tree...
error: patch failed: file1.c:6
error: file1.c: patch does not apply
Did you hand edit your patch?
It does not apply to blobs recorded in its index.
Cannot fall back to three-way merge.
Patch failed at 0001 patch 1
When you have resolved this problem run "git am --resolved".
If you would prefer to skip this patch, instead run "git am --skip".
To restore the original branch and stop patching run "git am --abort".
$ git am -3 /c/temp/git/format_since_origin.patch
Applying: patch 1
Using index info to reconstruct a base tree...
error: patch failed: file1.c:6
error: file1.c: patch does not apply
您是否手动编辑了补丁?
它不适用于其索引中记录的 blob。
不能回退到三路合并。
Patch failed at 0001 patch 1
解决此问题后,请运行“git am --resolved”。
如果您希望跳过此补丁,请运行“git am --skip”。
要恢复原始分支并停止修补运行“git am --abort”。
Any idea why this patch is failing? Or is my method totally wrong?
知道为什么这个补丁失败了吗?还是我的方法完全错误?
Update:
更新:
It works with the following
它适用于以下
$ git am -3 --ignore-whitespace /c/temp/git/format_since_origin.patch Applying: patch 1 Applying: patch 2 Applying: patch 3
$ git am -3 --ignore-whitespace /c/temp/git/format_since_origin.patch 应用:补丁 1 应用:补丁 2 应用:补丁 3
Now, as suggested by Charles - if i try the git diff, i get the error as below.
现在,正如查尔斯所建议的那样 - 如果我尝试 git diff,我会收到如下错误。
$ git diff -p origin > ../dif_origin.patch
$ git diff -p origin > ../dif_origin.patch
On applying,
在申请时,
$ git apply --ignore-whitespace --inaccurate-eof /c/temp/git/dif_origin.patch
c:/temp/git/dif_origin.patch:9: trailing whitespace.
patch change for file1.c
c:/temp/git/dif_origin.patch:18: trailing whitespace.
patch this xml guy
c:/temp/git/dif_origin.patch:29: trailing whitespace.
fsdfsd
c:/temp/git/dif_origin.patch:30: trailing whitespace.
patch this report
error: patch failed: file1.c:6
error: file1.c: patch does not apply
error: patch failed: file1.xml:2
error: file1.xml: patch does not apply
error: patch failed: tr/test_report.txt:2
error: tr/test_report.txt: patch does not apply
$ 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: 尾随空格。
patch 这个
报错:patch failed:file1.c:6
错误:file1.c:patch does not apply
错误:patch failed:file1.xml:2
错误:file1.xml:patch does not apply
错误:patch failed: tr/ test_report.txt:2
错误:tr/test_report.txt:补丁不适用
采纳答案by VonC
Note that one rationale for having to ignore whitespace was (June 2010):
请注意,必须忽略空格的一个理由是(2010 年 6 月):
What it does is enable the
GMail -> download -> git-am workflow
.
GMail (and doubtless countless other) E-Mail providers introduce whitespace at the beginning of raw E-Mail messages, while otherwise leaving them intact.
它的作用是启用
GMail -> download -> git-am workflow
.
GMail(无疑还有无数其他)电子邮件提供商在原始电子邮件消息的开头引入了空格,而在其他情况下则保持它们完整无缺。
As mentioned in "git am/format-patch
: control format of line endings", you can try a:
如“ git am/format-patch
:行尾控制格式”中所述,您可以尝试:
git am --keep-cr
That wouldn't require you to ignore whitespace (warning only).
这不需要您忽略空格(仅警告)。
The OP maxmelbinconfirms in the commentsthat the following works:
该OP maxmelbin确认在评论即以下工作:
git am -3 --keep-cr --committer-date-is-author-date /c/temp/git/format_since_origin.patch
回答by Manish Shrivastava
When git apply is working normally, you get no output at all:
当 git apply 正常工作时,您根本没有任何输出:
$ git apply example.patch
[nothing returned]
If you want to see what's going on behind the scenes, you can use the -v (verbose) flag:
如果你想看看幕后发生了什么,你可以使用 -v (verbose) 标志:
$ git apply -v example.patch
Checking patch includes/common.inc...
Applied patch includes/common.inc cleanly.
However, if running git apply from within your own local git working copy, it's possible that even with the -v (verbose) flag, git apply will do nothing, and give no output. If this happens, please check your location in the directory tree - git apply may work from another location.
但是,如果在您自己的本地 git 工作副本中运行 git apply ,则可能即使使用 -v (详细)标志, git apply 也不会执行任何操作,并且不提供任何输出。如果发生这种情况,请检查您在目录树中的位置 - git apply 可能在另一个位置工作。
An alternative to git apply is to use the patch command:
git apply 的替代方法是使用 patch 命令:
$ patch -p1 < example.patch
Here is other output the git apply command can generate, and what it means. Patch does not apply
这是 git apply 命令可以生成的其他输出及其含义。补丁不适用
$ git apply example.patch
error: patch failed: includes/common.inc:626
error: includes/common.inc: patch does not apply``
Git couldn't apply the changes in the patch because it wasn't able to find the line(s) of code in question; they must have been changed or removed by another commit. Try these things:
Git 无法应用补丁中的更改,因为它无法找到有问题的代码行;它们必须已被另一个提交更改或删除。试试这些:
Make sure the patch hasn't already been applied. Look for it in git-log or simply examine the code to see if the change(s) are already present. If they are, you're done. If they aren't or if only some of them are, try something else:
确保尚未应用补丁。在 git-log 中查找它或简单地检查代码以查看更改是否已经存在。如果他们是,你就完成了。如果它们不是或只有其中一些是,请尝试其他方法:
Use patch -p1 < filename.patch
. Whereas git-apply altogether rejects a patch with any errors, patch -p1 works hunk by hunk, applying as many individual changes as it can. It backs up each file as filename.ext.orig before modifying it and saves rejected hunks in filename.ext.rej
. Discard the .orig files and manually apply the changes left in the .rej. This is an easy strategy for small patches.
使用patch -p1 < filename.patch
. git-apply 完全拒绝带有任何错误的补丁,而补丁 -p1 则是一个一个地工作,应用尽可能多的单个更改。它在修改每个文件之前将其备份为 filename.ext.orig 并将被拒绝的大块保存在filename.ext.rej
. 丢弃 .orig 文件并手动应用 .rej 中留下的更改。对于小补丁来说,这是一个简单的策略。
回答by maxmelbin
ok. the following worked .
好的。以下工作。
$ git am -3 --ignore-whitespace /c/temp/git/format_since_origin.patch
Applying: patch 1
Applying: patch 2
Applying: patch 3
$ git am -3 --ignore-whitespace /c/temp/git/format_since_origin.patch
应用:补丁 1
应用:补丁 2
应用:补丁 3
Why would i need to ignore whitespace while applying?
为什么我在申请时需要忽略空格?