问题
Not too long ago, I asked this question. I received an answer, but it did not solve the issue I was having, so I was advised to create a new question with more detail.
Just to reiterate, I am getting an error when trying to manually edit a hunk in the Git bash shell on Windows, when using git add -p
(specifically, I press e
when prompted by the interactive command). This opens an edit file for the hunk in Notepad++ (my default .txt editor). Even without making any changes to this file, I am given the error:
Your edited hunk does not apply
As a minimal example, I have created a repo, value of git config core.autocrlf
is false
(I've also tried the following with value set to true
). I have a .txt file (foobar.txt
), I write foo
in it, and commit my changes. Then I change the contents of the file to:
foo
bar
and then close the file. I then do git add -p foobar
, and am prompted (not sure why the diff didn't work out cleanly in this case, but it generally does work for me):
diff --git a/foobar b/foobar
index 1910281..a907ec3 100644
--- a/foobar
+++ b/foobar
@@ -1 +1,2 @@
-foo
\ No newline at end of file
+foo
+bar
\ No newline at end of file
Stage this hunk [y,n,q,a,d,/,e,?]?
to which I answer e
. Again, without making any changes I get the error:
error: patch failed: foobar:1
error: foobar: patch does not apply
Your edited hunk does not apply. Edit again (saying "no" discards!) [y/n]?
Of course, I have also tried making changes to the manual hunk edit file; for example, the edit file will initially look like:
# Manual hunk edit mode -- see bottom for a quick guide
@@ -1 +1,2 @@
-foo
\ No newline at end of file
+foo
+bar
\ No newline at end of file
# ---
# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.
#
# If the patch applies cleanly, the edited hunk will immediately be
# marked for staging. If it does not apply cleanly, you will be given
# an opportunity to edit again. If all lines of the hunk are removed,
# then the edit is aborted and the hunk is left unchanged.
A sample edit would be:
# Manual hunk edit mode -- see bottom for a quick guide
@@ -1 +1,1 @@
-foo
\ No newline at end of file
+foo
\ No newline at end of file
# ---
# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.
#
# If the patch applies cleanly, the edited hunk will immediately be
# marked for staging. If it does not apply cleanly, you will be given
# an opportunity to edit again. If all lines of the hunk are removed,
# then the edit is aborted and the hunk is left unchanged.
Another interesting point is that I have a developer's release of Windows, which allows me to use "Bash on Ubuntu on Windows". Using git add -p
through this utility, and then trying to manually edit hunks, will work... so my question is, why does it not work on Git bash shell for me...?
回答1:
I've just had the same problem; I have core.autocrlf set to true, and am using Notepad++ as my editor.
I have all whitespace made visible, and noticed that in the # comments above and below the hunk, added by Git, use LF, but the hunk itself uses CR-LF. So I applied Edit->EOL Conversion->Windows Format. I could then apply the hunk, with or without edits.
回答2:
Even without making any changes to this file, I am given the error:
Your edited hunk does not apply
That error might be less frequent with Git 2.17 (Q2 2018).
Before that, "git add -p
" has been lazy in coalescing split patches before passing the result to underlying "git apply", leading to corner case bugs; the logic to prepare the patch to be applied after hunk selections has been tightened.
See commit 3a8522f, commit b3e0fcf, commit 2b8ea7f (05 Mar 2018), commit fecc6f3, commit 23fea4c, commit 902f414 (01 Mar 2018), and commit 11489a6, commit e4d671c, commit 492e60c (19 Feb 2018) by Phillip Wood (phillipwood).
(Merged by Junio C Hamano -- gitster -- in commit 436d18f, 14 Mar 2018)
add -p
: fix counting when splitting and coalescingWhen a file has no trailing new line at the end diff records this by appending "
\ No newline at end of file
" below the last line of the file.
This line should not be counted in the hunk header.Fix the splitting and coalescing code to count files without a trailing new line properly and change one of the tests to test splitting without a trailing new line.
来源:https://stackoverflow.com/questions/41972175/git-bash-shell-on-windows-your-edited-hunk-does-not-apply-continued