13

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?

BiAiB
  • 12,932
  • 10
  • 43
  • 63
  • 5
    Be sure that each context line start with a leading space char ' ', even if the line is otherwise empty. – Yann Droneaud Feb 29 '12 at 16:33
  • 3
    Have a browse through [this question](http://stackoverflow.com/questions/3268596/git-add-interactive-your-edited-hunk-does-not-apply). – simont Feb 29 '12 at 21:08

2 Answers2

17

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.

antho
  • 943
  • 2
  • 8
  • 11
  • 2
    You have to adjust the liner numbers in @@ @@ at the top if the number of lines change. – Niklas R Sep 04 '15 at 13:24
  • 5
    @NiklasR as of Git version 2.11.0, you DON'T need to adjust the liner numbers in @@ @@ at the top, because making changes to lines that are not modified **MUST** be in the actual code rather than in the interactive mode manual editing. The git interactive mode will handle the modified lines adjusts by itself. – Eido95 Apr 18 '17 at 20:35
  • 2
    The keyword for me was _replace_. I just removed the '-' character (not the whole line), and then the patch did not apply either. Removing the '-' and adding a space did the trick. – Johan Persson Mar 19 '20 at 16:45
  • 1
    exactly same thing as johan persson, also no need to edit the header hunk numbers – Barbz_YHOOL Jun 21 '20 at 19:41
1

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 @@ @@,

Tiina
  • 4,285
  • 7
  • 44
  • 73