git 修复后如何应用被拒绝的帅哥?

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

How do I apply rejected hunks after fixing them?

gitconflictpatchgit-apply

提问by eckes

I'm trying to apply a patch to a file using git apply. The overall patch failed, so I used git apply --reject.

我正在尝试使用git apply. 整体补丁失败,所以我使用了git apply --reject.

Inspecting the generated .rejfile showed me what's wrong, now I fixed the problem in the .rejfile.

检查生成的.rej文件告诉我出了什么问题,现在我修复了.rej文件中的问题。

But trying to apply the .rejfile fails with message

但是尝试应用.rej文件失败并显示消息

fatal: patch fragment without header at line 2: ...

致命:第 2 行没有标题的补丁片段:...

Is there a way to re-apply the .rejfile after fixing the problems there?Or do I have to modify the original patch and have to re-run git apply?

有没有办法.rej在修复那里的问题后重新应用文件?还是我必须修改原始补丁并重新运行git apply

This would be a bit cumbersome in that case since the original patch contains patches for dozens of files and I don't want to git checkoutthe applied modifications in order to re-git applythe whole fixed patch file.

在那种情况下这会有点麻烦,因为原始补丁包含数十个文件的补丁,我不想git checkout应用修改来重新git apply修复整个固定补丁文件。

采纳答案by drzaus

To clarify what @julian-squires said, the problem is that the .rejfiles are missing some minor stuff between diff a/thefile...and @@ -line/columns....

为了澄清@julian-squires 所说的内容,问题在于.rej文件在diff a/thefile...和之间缺少一些小东西@@ -line/columns...

ORIGINAL .rejfile

原始.rej文件

diff a/the/original/file.cs b/the/original/file.cs    (rejected hunks)
@@ -27,9 +27,9 @@ whatever was on that line
diff a/the/original/file.cs b/the/original/file.cs    (rejected hunks)
@@ -27,9 +27,9 @@ whatever was on that line

You need to copy the a/b filenames from the diffline and add them with the change indicators below, like:

您需要从该diff行复制 a/b 文件名,并使用以下更改指示符添加它们,例如:

UPDATED .rejfile

更新.rej文件

diff a/the/original/file.cs b/the/original/file.cs    (rejected hunks)
--- a/the/original/file.cs
+++ b/the/original/file.cs
@@ -27,9 +27,9 @@ whatever was on that line
diff a/the/original/file.cs b/the/original/file.cs    (rejected hunks)
--- a/the/original/file.cs
+++ b/the/original/file.cs
@@ -27,9 +27,9 @@ whatever was on that line

Then you can apply the .rejfiles like a regular patch.

然后您可以.rej像应用常规补丁一样应用这些文件。

回答by Julian Squires

I had this problem recently, while using git am --rejectto apply a bunch of patches. The way I approached it was to massage the .rejfile header into something patch(1)would recognize, with

我最近遇到了这个问题,同时git am --reject用来应用一堆补丁。我处理它的方法是将.rej文件头按摩成patch(1)可以识别的东西,用

sed -e 's/^diff a\/\(.*\) b\/\(.*\)[[:space:]].*rejected.*$/--- \n+++ /'

and modified them with emacs (whose diff-modewill update the line counts in the hunks as you modify the patch) and applied them with patch.

并用 emacs 修改它们(diff-mode当你修改补丁时,它会更新大块中的行数)并用patch.

My workflow ended up looking like this:

我的工作流程最终看起来像这样:

$ (for i in $(find . -name \*.rej); do
     sed -e 's/^diff a\/\(.*\) b\/\(.*\)[[:space:]].*rejected.*$/--- \n+++ /' -i $i &&
     emacsclient $i &&
     patch -p0 < $i;
   done) && git add -u && git clean -xdf && git am --continue

with suitable macros setup in emacs for the recurring cases. Not the most elegant approach, but it worked.

在 emacs 中为重复出现的情况设置合适的宏。不是最优雅的方法,但它奏效了。

回答by cforbish

There is no way around having to manually modify the files where there is a .rejfile. You said that you did fix this. Once all of the .rejissues have been taken care of you are ready for git commit. git apply --rejectstill saves a little time in that git apply --rejectwill modify files where it can.

没有办法在有文件的地方手动修改.rej文件。你说你确实解决了这个问题。一旦所有.rej问题都得到解决,您就可以开始了git commitgit apply --reject仍然可以节省一点时间,git apply --reject因为它可以修改文件。