git:补丁不适用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4770177/
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: patch does not apply
提问by Dmitrii Pisarenko
I have a certain patch called my_pcc_branch.patch.
我有一个名为 my_pcc_branch.patch 的补丁。
When I try to apply it, I get following message:
当我尝试应用它时,我收到以下消息:
$ git apply --check my_pcc_branch.patch
warning: src/main/java/.../AbstractedPanel.java has type 100644, expected 100755
error: patch failed: src/main/java/.../AbstractedPanel.java:13
error: src/main/java/.../AbstractedPanel.java: patch does not apply
What does it mean?
这是什么意思?
How can I fix this problem?
我该如何解决这个问题?
回答by user1028904
git apply --reject --whitespace=fix mychanges.patch
worked for me.
git apply --reject --whitespace=fix mychanges.patch
对我来说有效。
Explanation
解释
The --reject
option will instruct git to not fail if it cannot determine how to apply a patch, but instead to apply indivdual hunks it can apply and create reject files (.rej
) for hunks it cannot apply. Wigglecan "apply [these] rejected patches and perform word-wise diffs".
--reject
如果无法确定如何应用补丁,该选项将指示 git 不会失败,而是应用它可以应用的单个大块并.rej
为它不能应用的大块创建拒绝文件 ( )。Wiggle可以“应用 [这些] 被拒绝的补丁并执行逐字差异”。
Additionally, --whitespace=fix
will warn about whitespace errors and try to fix them, rather than refusing to apply an otherwise applicable hunk.
此外,--whitespace=fix
将警告空白错误并尝试修复它们,而不是拒绝应用其他适用的大块。
Both options together make the application of a patch more robust against failure, but they require additional attention with respect to the result.
这两个选项一起使补丁的应用程序在失败时更加稳健,但它们需要额外关注结果。
For the whole documentation, see https://git-scm.com/docs/git-apply.
有关整个文档,请参阅https://git-scm.com/docs/git-apply。
回答by Dmitrii Pisarenko
Johannes Sixt from the [email protected] mailing list suggested using following command line arguments:
来自 [email protected] 邮件列表的 Johannes Sixt 建议使用以下命令行参数:
git apply --ignore-space-change --ignore-whitespace mychanges.patch
This solved my problem.
这解决了我的问题。
回答by ruffin
When all else fails, try git apply
's --3way
option.
当所有其他方法都失败时,请尝试git apply
's --3way
option。
git apply --3way patchFile.patch
git apply --3way patchFile.patch
--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. This option implies the --index option, and is incompatible with the --reject and the --cached options.
--3way
当补丁没有完全应用时,如果补丁记录了它应该应用的 blob 的身份,并且我们在本地有这些 blob,可能会在文件中留下冲突标记,则回退到 3 路合并供用户解析的工作树。此选项暗含 --index 选项,并且与 --reject 和 --cached 选项不兼容。
Typical fail case applies as much of the patch as it can, and leaves you with conflicts to work out in git however you normally do so. Probably one step easier than the reject
alternative.
典型的失败情况会尽可能多地应用补丁,并让您在 git 中解决冲突,但您通常会这样做。可能比reject
另一种方法更容易一步。
回答by Ivan Voroshilin
This command will apply the patch not resolving it leaving bad files as *.rej
:
此命令将应用补丁而不解决它,留下坏文件*.rej
:
git apply --reject --whitespace=fix mypath.patch
You just have to resolve them. Once resolved run:
你只需要解决它们。解决后运行:
git -am resolved
回答by Pini Cheyni
Try using the solution suggested here: https://www.drupal.org/node/1129120
尝试使用此处建议的解决方案:https: //www.drupal.org/node/1129120
patch -p1 < example.patch
patch -p1 < example.patch
This helped me .
这帮助了我。
回答by Ben Hymanson
It happens when you mix UNIX and Windows git clients because Windows doesn't really have the concept of the "x" bit so your checkout of a rw-r--r--
(0644) file under Windows is "promoted" by the msys POSIX layer to be rwx-r-xr-x
(0755). git considers that mode difference to be basically the same as a textual difference in the file, so your patch does not directly apply. I think your only good option here is to set core.filemode
to false
(using git-config
).
当您混合使用 UNIX 和 Windows git 客户端时会发生这种情况,因为 Windows 并没有真正具有“x”位的概念,因此您rw-r--r--
在 Windows 下签出(0644) 文件被 msys POSIX 层“提升”为rwx-r-xr-x
(0755) . git 认为模式差异与文件中的文本差异基本相同,因此您的补丁不直接适用。我认为您在这里唯一的好选择是设置core.filemode
为false
(using git-config
)。
Here's a msysgit issue with some related info: http://code.google.com/p/msysgit/issues/detail?id=164(rerouted to archive.org's 3 Dec 2013 copy)
这是一个带有一些相关信息的 msysgit 问题:http: //code.google.com/p/msysgit/issues/detail?id=164 (重新路由到 archive.org 2013 年 12 月 3 日的副本)
回答by Ophidian
In my case I was stupid enough to create the patch file incorrectly in the first place, actually diff-ing the wrong way. I ended up with the exact same error messages.
就我而言,我很愚蠢,一开始就错误地创建了补丁文件,实际上以错误的方式进行了区分。我最终得到了完全相同的错误消息。
If you're on master and do git diff branch-name > branch-name.patch
, this tries to remove all additions you want to happen and vice versa (which was impossible for git to accomplish since, obviously, never done additions cannot be removed).
如果您在 master 和 do 上git diff branch-name > branch-name.patch
,这会尝试删除您想要发生的所有添加,反之亦然(这对于 git 来说是不可能完成的,因为显然,无法删除从未完成的添加)。
So make sure you checkout to your branch and execute git diff master > branch-name.patch
因此,请确保您结帐到您的分支并执行 git diff master > branch-name.patch
回答by Archmede
回答by Bhanu
What I looked for is not exactly pointed out in here in SO, I'm writing for the benefit of others who might search for similar. I faced an issue with one file (present in old repo) getting removed in the repo. And when I apply the patch, it fails as it couldn't find the file to be applied. (so my case is git patch fails for file got removed) '#git apply --reject' definitely gave a view but didn't quite get me to the fix. I couldn't use wiggle as it is not available for us in our build servers. In my case, I got through this problem by removing the entry of the 'file which got removed in the repo' from patch file I've tried applying, so I got all other changes applied without an issue (using 3 way merge, avoiding white space errors), And then manually merging content of file removed into where its moved.
我所寻找的并没有在 SO 中完全指出,我正在写作是为了其他可能寻找类似的人的利益。我遇到了一个文件(存在于旧仓库中)在仓库中被删除的问题。当我应用补丁时,它失败了,因为它找不到要应用的文件。(所以我的情况是 git patch 因文件被删除而失败)“#git apply --reject”确实给出了一个观点,但并没有让我得到解决。我无法使用 wiggle,因为它在我们的构建服务器中不可用。在我的情况下,我通过从我尝试应用的补丁文件中删除“在 repo 中删除的文件”的条目来解决这个问题,所以我没有问题地应用了所有其他更改(使用 3 路合并,避免空白错误),然后手动将删除的文件内容合并到其移动的位置。