使用 git 应用差异文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15934101/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:56:51  来源:igfitidea点击:

Applying a diff file with git

gitgithub

提问by George Wilson

So I was trying to use apply a diff file to my git dev branch. The diff I wanted to apply was this one here: https://github.com/mbabker/joomla-cms/compare/JHtml_move.patch

所以我试图将 diff 文件应用于我的 git dev 分支。我想应用的差异在这里:https: //github.com/mbabker/joomla-cms/compare/JHtml_move.patch

I used git apply PATH_TO_PATCH.patch

我用了 git apply PATH_TO_PATCH.patch

Now on trying to apply it I get a load of errors cumulating with a

现在在尝试应用它时,我得到了大量的错误累积

fatal: git apply: bad git-diff - expected /dev/null on line 47

Thing is line 47 reads --- /dev/null

事情是第 47 行读取 --- /dev/null

I saw on another forum somebody say there was a trailing white space after the /dev/nullpart but there isn't.

我在另一个论坛上看到有人说/dev/null零件后面有一个尾随的空白,但没有。

In case its any help I'm also being thrown errors of trailing white spaces on lines 9, 10, 11, 12 and 13 - despite their NOT being any trailing white spaces.

如果有任何帮助,我也会在第 9、10、11、12 和 13 行抛出尾随空格的错误 - 尽管它们不是任何尾随空格。

In any case the diff is being generated by git - I'm just copying and pasting it into notepad.

在任何情况下,差异都是由 git 生成的 - 我只是将其复制并粘贴到记事本中。

I've also tried using curl https://github.com/mbabker/joomla-cms/compare/JHtml_move.patch | git ambut that just gives an error without specifying location.

我也试过使用,curl https://github.com/mbabker/joomla-cms/compare/JHtml_move.patch | git am但这只是在没有指定位置的情况下出现错误。

Error message from curl attempt

来自 curl 尝试的错误消息

Any ideas as to why its giving an fatal error when the line actually exists????

关于为什么在该行实际存在时会出现致命错误的任何想法????



Update to @IvanZuzakI tried adding in the git am --ignore-space-change --ignore-whitespace PATH_TO_PATCH.patchhowever I got first of all (as this was the downloaded patch) an error saying the email was invalid (it had been converted to html code) so I tried to fix that but it kept throwing it up every now and again. Then it said previous rebase directory PATH_TO_GITHUB_BRANCH/.git/rebase-apply still exists but mbox given.. So I found thiswhich suggested removing the rebase-apply folder in the branches .git folder. So I did that and then I got the invalid email error again (despite I'd changed both ones away from the html formatted)

更新到@IvanZuzak我尝试添加git am --ignore-space-change --ignore-whitespace PATH_TO_PATCH.patch但是我首先得到一个错误(因为这是下载的补丁)一个错误说电子邮件无效(它已转换为html代码)所以我试图修复它但它一直抛出它时不时起来。然后它说previous rebase directory PATH_TO_GITHUB_BRANCH/.git/rebase-apply still exists but mbox given.。所以我发现这个建议删除分支 .git 文件夹中的 rebase-apply 文件夹。所以我这样做了,然后我又收到了无效的电子邮件错误(尽管我已经将两个都改成了 html 格式)

回答by Garrett

I also had the same problem:

我也遇到了同样的问题:

fatal: git apply: bad git-diff - expected /dev/null on line 47

Yet line 47 reads --- /dev/null. The problem I found was that the line endings were in Windows format instead of UNIX format. Converting the line endings to UNIX format in Notepad++ fixed the problem for me.

然而第 47 行读取--- /dev/null。我发现的问题是行尾是 Windows 格式而不是 UNIX 格式。在 Notepad++ 中将行尾转换为 UNIX 格式为我解决了这个问题。

回答by bigosmallm

I had the same issue. I opened up Git Bash (Cygwin works as well) and did:

我遇到过同样的问题。我打开了 Git Bash(Cygwin 也可以)并做了:

dos2unix.exe <patch-file>

Then I was able to apply the patch perfectly fine.

然后我能够完美地应用补丁。

回答by VonC

You will have to check if you still see the error message bad git-diff - expected /dev/null on line 47after Git 2.17 (Q2 2018).

您必须检查bad git-diff - expected /dev/null on line 47在 Git 2.17(2018 年第二季度)之后是否仍然看到错误消息。

Many places in "git apply" knew that "/dev/null" that signals "there is no such file on this side of the diff" can be followed by whitespace and garbage when parsing a patch, except for one, which made an otherwise valid patch (e.g. ones from subversion) rejected.

git apply”中的许多地方都知道“ /dev/null”表示“在差异的这一侧没有这样的文件”在解析补丁时可以跟随空白和垃圾,除了一个,它制作了一个有效的补丁(例如来自颠覆)被拒绝。

See commit e454ad4(15 Feb 2018) by Tatyana Krasnukha (tkrasnukha).
See commit f16ef7b(15 Feb 2018) by Johannes Schindelin (dscho).
(Merged by Junio C Hamano -- gitster--in commit 177bd65, 28 Feb 2018)

请参阅Tatyana Krasnukha ( )提交的 e454ad4(2018 年 2 月 15 日。 请参阅Johannes Schindelin ( ) 的提交 f16ef7b(2018 年 2 月 15 日(由Junio C Hamano合并-- --commit 177bd65,2018 年 2 月 28 日)tkrasnukha
dscho
gitster

See also git-for-windows/git issue 1489

另请参阅git-for-windows/git 问题 1489

apply: handle Subversion diffs with /dev/nullgracefully

Subversion generates diffs that can contain lines like this one:

--- /dev/null (nonexistent)

Let's teach Git's apply machinery to handle such a line gracefully.

apply:/dev/null优雅地处理 Subversion 差异

Subversion 生成的差异可以包含这样的行:

--- /dev/null(不存在)

让我们教 Git 的应用机制优雅地处理这样的一行。

回答by Aaron Skomra

I had the same issue, but I was testing a patch I was going to send to a listserve, following the recommendations here: https://www.kernel.org/doc/Documentation/email-clients.txt. I couldn't just fix the problem on my local file, I needed to distribute a patch which was potentially going to be mangled by gmail and I was checking the gmailed version with git apply.

我遇到了同样的问题,但我正在测试我要发送到列表服务器的补丁,遵循以下建议:https: //www.kernel.org/doc/Documentation/email-clients.txt。我不能只解决本地文件上的问题,我需要分发一个补丁,该补丁可能会被 gmail 破坏,并且我正在使用git apply.

My co-worker pointed out to me that

我的同事向我指出

git am 

Is better than git applyat correcting for these sorts of problems and is what should be used with .patchfiles. This is mentioned in one of the comments above but it should be an answer.

git apply纠正这些类型的问题更好,并且应该与.patch文件一起使用。上面的评论之一中提到了这一点,但这应该是一个答案。

回答by sigod

I had the same issue.

我遇到过同样的问题。

fatal: git apply: bad git-diff - expected /dev/null on line 96

fatal: git apply: bad git-diff - expected /dev/null on line 96

Or, if I convert all line-endings to Unix format:

或者,如果我将所有行结尾转换为 Unix 格式:

error: patch failed: <file>:81
error: <file>: patch does not apply
...

git apply --ignore-whitespace <patch>.patchworked with Unix line-endings.

git apply --ignore-whitespace <patch>.patch使用 Unix 行尾。

回答by mariovials

I had the same problem You have a rebase in progress, write

我遇到了同样的问题 你有一个 rebase 正在进行中,写

git rebase --skip 

many times, until see No rebase in progress?then try again with git am

很多次,直到看到No rebase in progress?然后再试一次git am