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?

10
  • Can you give the exact sequence of commands and other actions which lead to this error? Commented Jan 29, 2017 at 5: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. Commented Jan 29, 2017 at 7: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. Commented Jan 29, 2017 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? Commented Jan 29, 2017 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. Commented Jan 30, 2017 at 6:03

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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 What is the result of git diff -- afile? (with afile the one you are trying to add -p)
I get diff --git a/<file-a> b/<file-b> index <short-hash-a>..<short-hash-b> 100644 --- a/<file-a> +++ b/<file-b> <changes>
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 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 stackoverflow.com/a/41310550/6309.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.