Windows 上的 git bash shell “Your edited hunk does not apply”(续)

标签 git shell interactive git-add

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

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

Your edited hunk does not apply


作为一个最小的示例,我创建了一个存储库,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]?

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

# 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 ,然后尝试手动编辑 hunks ,将会起作用...所以我的问题是,为什么它在我的 Git bash shell 上不起作用...?

最佳答案

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

我将所有空白都设为可见,并注意到在由 Git 添加的 block 上方和下方的 # 注释中,使用 LF,但 block 本身使用 CR-LF。所以我应用了“编辑”->“EOL 转换”->“Windows 格式”。然后我就可以应用这个大块,无论是否进行编辑。

关于Windows 上的 git bash shell “Your edited hunk does not apply”(续),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41972175/

相关文章:

git - .gitignore文件中 `syntax: glob/regexp`是什么意思?

linux - 如何在删除 .git 文件夹后 "read-only"- 恢复本地 GIT 存储库和文件引用?

python - 使用 Paramiko 在 Python 中通过 ssh 实现交互式 shell?

git - 避免忽略 git 中的文件夹

git wildcard - 删除子目录的所有实例

bash - fork /多线程进程 |狂欢

linux - 在文件的每一行前面打印文件名

perl - 如果磁盘空间不足,如何在不存在截断文件的危险的情况下从 shell 脚本重写文件?

ios - 以交互方式关闭模态视图 Controller 时出现故障

bash - 如何从 tcl 脚本调用 bash 命令?