BiAiB
BiAiB

Reputation: 14122

editing git patch gives "Your edited hunk does not apply"

I'm adding a file interactively:

git add ../../template/panels/panel-reports.php -p
diff --git a/template/panels/panel-reports.php b/template/panels/panel-reports.php
index 5482228..48d2901 100644
--- a/template/panels/panel-reports.php
+++ b/template/panels/panel-reports.php
@@ -214,6 +214,8 @@

                        <a class="addCategory"></a>
                        <a class="removeCategory"></a>
+                       <a class="addDocument"></a>
+                       <a class="checkTool"></a>

                        <div class="categoriesList"></div>
                        <div class="documentsList"></div>
Stage this hunk [y,n,q,a,d,/,e,?]? e

ps: those are the only modified lines here

I want to remove

+                       <a class="checkTool"></a>

so I edit like this:

# Manual hunk edit mode -- see bottom for a quick guide
@@ -214,6 +214,7 @@

                                                <a class="addCategory"></a>
                                                <a class="removeCategory"></a>
+                                               <a class="addDocument"></a>

                                                <div class="categoriesList"></div>
                                                <div class="documentsList"></div>
# ---
# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.

and git refused it:

error: patch failed: template/panels/panel-reports.php:214
error: template/panels/panel-reports.php: patch does not apply
Your edited hunk does not apply. Edit again (saying "no" discards!) [y/n]? 

To me I don't see any error or ambiguity in what I wrote, so where Am I wrong?

Upvotes: 13

Views: 6073

Answers (2)

Tiina
Tiina

Reputation: 4765

My up to date version Git, 2.33.1.windows.1, git add -p does not have a subcommand s, so I used e.

Error Your edited hunk does not apply is because I misunderstood its bottom hints:

# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.

https://git-scm.com/docs/git-add#_editing_patches gives a more detailed introduction:

added content

Added content is represented by lines beginning with "+". You can prevent staging any addition lines by deleting them. (delete lines, not "+" mark. )

removed content

Removed content is represented by lines beginning with "-". You can prevent staging their removal by converting the "-" to a " " (space). (replace "-" mark with a " " space mark)

modified content

Modified content is represented by "-" lines (removing the old content) followed by "+" lines (adding the replacement content). You can prevent staging the modification by converting "-" lines to " ", and removing "+" lines. Beware that modifying only half of the pair is likely to introduce confusing changes to the index. (combination of the former two)

And nothing else! no need to modify numbers between @@ @@,

Upvotes: 1

antho
antho

Reputation: 963

ydroneaud's comment was the answer I was looking for.

Many text editors cut trailing white spaces when saving, look out for that.

Second thing, when removing a "-" to keep a line, actually replace it with a space " ", don't just delete it.

Last thing, don't fiddle with the numbers in @@ @@ at the top.

Upvotes: 17

Related Questions