Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top