25

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

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

But trying to apply the .rej file fails with message

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

Is there a way to re-apply the .rej file after fixing the problems there? Or do I have to modify the original patch and have to re-run 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 checkout the applied modifications in order to re-git apply the whole fixed patch file.

eckes
  • 64,417
  • 29
  • 168
  • 201

3 Answers3

21

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

ORIGINAL .rej file

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 diff line and add them with the change indicators below, like:

UPDATED .rej file

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 .rej files like a regular patch.

Community
  • 1
  • 1
drzaus
  • 24,171
  • 16
  • 142
  • 201
  • Thanks for the explanation. I'll check your answer because it's plain English instead of plain Bash... – eckes Feb 17 '15 at 20:04
4

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

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

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

My workflow ended up looking like this:

$ (for i in $(find . -name \*.rej); do
     sed -e 's/^diff a\/\(.*\) b\/\(.*\)[[:space:]].*rejected.*$/--- \1\n+++ \2/' -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.

Julian Squires
  • 437
  • 4
  • 8
0

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

Étienne
  • 4,773
  • 2
  • 33
  • 58
cforbish
  • 8,567
  • 3
  • 28
  • 32