Search code examples
gitinteractivegit-add

git bash shell on Windows "Your edited hunk does not apply"


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?


Solution

  • 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.