12kesselrun
12kesselrun

Reputation: 81

How do I avoid "error: patch fragment without header at line X" for the following hunk when manually editing hunks in Git?

I am running Git version 2.18.0.windows.1 and trying to manually edit hunks (this is my first time messing with this). I started by splitting a larger hunk and this is the first of the two hunks. I went to edit the first hunk as follows...

Old Hunk:

@@ -1,8 +1,8 @@
 Shopping List

-apples
-bananas
-yogurt
-milk
+red apples
+6 bananas
+vanilla yogurt
+2% milk
 wheat bread
 cereal

Attempted Edits:

@@ -1,8 +1,8 @@
 Shopping List

 apples
-bananas
+6 bananas
 yogurt
-milk
+2% milk
 wheat bread
 cereal

...but when I save and close my text editor (Atom) Git gives me the following error message:

error: patch fragment without header at line 16: @@ -7,6 +7,6 @@

The line numbers in the error message correspond to the line numbers of my second hunk so I'm guessing that since I changed the line numbers when editing the first hunk then it must have screwed with the line numbers for the second hunk. The odd part is that I'm following a course and the instructor doesn't seem to have an issue. Is this a potential bug in the version of Git that I'm running or am I missing something?

NOTE: I made sure that there are proper spaces/+/- in front of each line and attempted to modify the line numbers when editing the first hunk but to no avail.

Upvotes: 8

Views: 6052

Answers (1)

Hi-Angel
Hi-Angel

Reputation: 5659

Typical reasons for the error is either a lack of one space before a context line (note that lines start with either of -, +, or a space), or a lack of the lines with filename that describe which file is being modified.

Judging by your "steps to reproduce", it is clear you are missing the filename, which will cause the error mentioned in title. By simply adding 2 lines with the filename:

--- a/list
+++ b/list

I made your edited version work, see:

 λ cat 1
--- a/list
+++ b/list
@@ -1,8 +1,8 @@
 Shopping List

 apples
-bananas
+6 bananas
 yogurt
-milk
+2% milk
 wheat bread
 cereal
 λ git apply ./1
 λ git --no-pager diff
diff --git a/list b/list
index 9d00b85..d4c66c5 100644
--- a/list
+++ b/list
@@ -1,8 +1,8 @@
 Shopping List

 apples
-bananas
+6 bananas
 yogurt
-milk
+2% milk
 wheat bread
 cereal

P.S.: I do note that the error you show in the question body refers to line 16, which doesn't even exist. That implies it belongs to a different diff. In any case, as far as the one you posted is concerned, this is the solution.

Upvotes: 0

Related Questions