How do I apply rejected hunks after fixing them?

eckes picture eckes · Jul 26, 2013 · Viewed 27.2k times · Source

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.

Answer

drzaus picture drzaus · Feb 17, 2015

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.