1

I have a very frustrating error when trying to manually edit hunks while using the command:

git add -p

I have already looked through many of the existing questions on this site, including this one, and this one.

I am at the point where I try to manually edit a hunk, make no changes to the file opened by git for manual editing, and still getting the error "Your edited hunk does not apply".

I thought this might have to do with CRLF vs. LF EOL characters. I am editing in Notepad++, and I can see that all of my line endings are CRLF. I naively tried to change all the line endings to LF, but to no avail.

I can also see that all context lines have leading spaces, so that isn't the issue either. Furthermore, I have:

git --global core.autocrlf

set to true.

Any help is greatly appreciated.


Edit: According to VonC's answer, in my specific case I have a permission + content diff. Does this still mean I cannot do:

git add -p

If it were only a permission diff, I understand why this wouldn't be necessary, and the interactive option must be dropped; but in this case, is there a way to still do an interactive add?

Community
  • 1
  • 1
Atreyu
  • 639
  • 2
  • 7
  • 16
  • Can you give the exact sequence of commands and other actions which lead to this error? – Code-Apprentice Jan 29 '17 at 05:37
  • After running `git add -p`, I just type `e` to manually edit a hunk of code. `git` then opens an edit file for editing the hunk. I get the error even if I close this edit file without making any changes. – Atreyu Jan 29 '17 at 07:29
  • Please edit your question to include this information. You might also want to add the step that you edit your code. It will also be helpful to give a more concrete example of the original edit and then later show how you edit the hunk. – Code-Apprentice Jan 29 '17 at 14:28
  • Also, when you say that "git then opens an edit file", I assume you mean that git opens the file in vim. Is that correct? – Code-Apprentice Jan 29 '17 at 14:29
  • @Code-Apprentice I have included all this information in my question. Note that I said git open my edit file in Notepad++ (my default editor for .txt files). I also say that I get the error even after no changes were made to the hunk edit file. – Atreyu Jan 30 '17 at 06:03
  • You still can make a `git add -p` for any file with content diff: `git add -p -- a_file_with_content_diff`. – VonC Jan 30 '17 at 06:14
  • @VonC this is exactly what is not working for me – Atreyu Jan 30 '17 at 06:17
  • Hence the need for a new question clearly illustrating the issue – VonC Jan 30 '17 at 06:19
  • @VonC But I feel like it should still be this same question... this question does not assume I have only a permission change in the file I am trying to commit (in fact, I was never trying to `git add -p` such a file). Do you still think I should open a new question? – Atreyu Jan 30 '17 at 06:26
  • Yes, with a link back to this one, and a lot more details, like git version as well as a small she'll transcript illustrating the persisting issue. – VonC Jan 30 '17 at 06:34

1 Answers1

1

First, set git --global core.autocrlf to false.
Then clone again your repo, and check the value of core.autocrlf:

cd /path/to/new/clone
git config core.autocrlf

(no --global)

Finally, try again your git add -p (after reporting your modification in your new working tree).

Regarding hunk management, see also "Unexpected result in git-diff".


"Your edited hunk does not apply".

diff --git a/<file-a> b/<file-b> > 
index <short-hash-a>..<short-hash-b> 100644 
--- a/<file-a> 
+++ b/<file-b>
<changes>

If there was no changes (only in permission), adding a "patch" in that case does not make sense: you just git add the file (no -p)

But if there are changes, the question the OP mentioned "git add --interactive “Your edited hunk does not apply”" remains the reference.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for your answer! I have tried what you have said. The local value for `core.autocrlf` is returned as `false` in my new cloned repo, but `git add -p` in manual edit mode still returns the same error (again without any changes made to the edit file). – Atreyu Jan 29 '17 at 07:15
  • @Atreyu What is the result of `git diff -- afile`? (with `afile` the one you are trying `to add -p`) – VonC Jan 29 '17 at 07:35
  • I get `diff --git a/ b/` `index .. 100644` `--- a/` `+++ b/` `` – Atreyu Jan 29 '17 at 07:50
  • I'm a bit confused, I have content changes in my file, and I don't recall changing permissions to the file... is this a quirk in Windows? – Atreyu Jan 29 '17 at 07:58
  • @Atreyu in your case, if you see only 100644 and actual content changes, this is not a permission issue (I revised my answer to make it clearer). Regarding permissions on Windows, see as an illustration http://stackoverflow.com/a/41310550/6309. – VonC Jan 29 '17 at 08:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/134336/discussion-between-atreyu-and-vonc). – Atreyu Jan 30 '17 at 05:49