Windows上的git bash shell“你编辑的hunk不适用”(续)

时间:2017-02-01 04:57:06

标签: git shell interactive git-add

不久前,我问this question。我收到了答复,但它没有解决我遇到的问题,因此建议我更详细地创建一个新问题。

重申一下,当我在使用git add -p时尝试手动编辑Windows上Git bash shell中的hunk时出现错误(具体来说,我在交互式命令提示时按e )。这将在Notepad ++(我的默认.txt编辑器)中打开hunk的编辑文件。即使没有对此文件进行任何更改,我也会收到错误:

  

您编辑的hunk不适用

作为一个最小的例子,我创建了一个repo,git config core.autocrlf的值为false(我还尝试了以下值,将值设置为true)。我有一个.txt文件(foobar.txt),我在其中写foo,并提交我的更改。然后我将文件的内容更改为:

foo 
bar

然后关闭该文件。然后我做了git add -p foobar,并且被提示(不确定为什么差异在这种情况下干净利落,但它通常对我有效):

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,?]?

我回答e。再次,没有做任何更改我得到错误:

error: patch failed: foobar:1
error: foobar: patch does not apply
Your edited hunk does not apply. Edit again (saying "no" discards!) [y/n]?

当然,我也试过更改手动hunk编辑文件;例如,编辑文件最初看起来像:

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

示例编辑将是:

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

另一个有趣的一点是,我有一个开发人员的Windows版本,它允许我在Windows上使用" Bash on Ubuntu"。使用git add -p通过这个实用程序,然后尝试手动编辑帅哥,将工作...所以我的问题是,为什么它不适用于我的Git bash shell ...?

2 个答案:

答案 0 :(得分:1)

我刚遇到同样的问题;我将core.autocrlf设置为true,并使用Notepad ++作为我的编辑器。

我将所有空格都显示出来,并注意到在Git上方和下方的#comments中,使用LF,但是hunk本身使用CR-LF。所以我应用了Edit-> EOL Conversion-> Windows格式。然后我可以应用hunk,无论是否有编辑。

答案 1 :(得分:0)

  

即使没有对此文件进行任何更改,我也会收到错误:

Your edited hunk does not apply

使用Git 2.17(2018年第二季度)时,该错误可能不那么频繁 在此之前,“git add -p”在将结果传递给基础“git apply”之前,在合并拆分补丁方面一直懒惰,导致了角落案例错误;在收紧大块选择之后准备要应用的补丁的逻辑。

请参阅commit 3a8522fcommit b3e0fcfcommit 2b8ea7f(2018年3月5日),commit fecc6f3commit 23fea4ccommit 902f414(2018年3月1日),和commit 11489a6commit e4d671ccommit 492e60c(2018年2月19日)Phillip Wood (phillipwood)(由Junio C Hamano -- gitster --合并于commit 436d18f,2018年3月14日)

  

add -p:在拆分和合并时修复计数

     

当文件末尾没有尾随新行时,差异记录了这一点   在“最后一行”下面附加“\ No newline at end of file”   文件。
  此行不应计入hunk标头中。

     

修复拆分和合并代码以计算文件而不正确地使用尾随新行并更改其中一个测试以测试拆分而不使用   尾随新线。