0

tldr: why am I getting error: ... patch does not apply when editing a hunk and removing a line, even though it looks like it should work?

I'm editing a hunk while doing git add -p, where I want to only add new line 1 (and not add new line 2):

# Manual hunk edit mode -- see bottom for a quick guide
@@ -1 +1,3 @@
 first line
+new line 1
+new line 2
# ---
# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.

I changed the + to a space for new line 2:

# Manual hunk edit mode -- see bottom for a quick guide
@@ -1 +1,3 @@
 first line
+new line 1
 new line 2
# ---
# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.

but that gives the error:

error: patch failed: myfile:1
error: myfile: patch does not apply
Your edited hunk does not apply. Edit again (saying "no" discards!) [y/n]? 

What is the problem?

Rob Bednark
  • 25,981
  • 23
  • 80
  • 125

1 Answers1

0

Delete the line instead.

Changing the + to a space is not the correct action.
Per the directions given by git (# To remove '+' lines, delete them. ), what is needed is to delete new line 2:

# Manual hunk edit mode -- see bottom for a quick guide
@@ -1 +1,3 @@
 first line
+new line 1
# (^^^ note that "new line 2" has been deleted ^^^)
# ---
# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.
Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
  • You also must edit the header: I think `@@ -1 +1,3 @@` should become `@@ -1 +1,2 @@` – phd Jul 01 '20 at 16:22
  • @phd: Git will re-count the lines, so that there's no need to edit the hunk-header. – torek Jul 01 '20 at 17:55