git add --interactive “您编辑的块不适用”

发布于 2024-09-09 17:09:56 字数 1022 浏览 2 评论 0原文

我尝试使用 git add --interactive 选择性地向索引添加一些更改,但我不断收到“您编辑的块不适用。请再次编辑...”消息。即使我选择 e 选项,我也会收到此消息,并立即保存/关闭我的编辑器。换句话说,如果根本不编辑该块,该补丁就不会应用。

这是我正在使用的确切示例(我正在尝试组合一个小演示):

原始文件:

first change
second change off branch
third change off branch
second change
third change
fourth change

新文件:

Change supporting feature 1
first change
second change off branch
third change off branch
second change
third change
fourth change
bug fix 1
change supporting feature 1

我正在尝试展示如何仅使用 git add --interactive将“bug fix 1”行添加到索引中。在文件上运行交互式添加,我选择补丁模式。它向我展示了

diff --git a/newfile b/newfile
index 6d501a3..8b81ae9 100644
--- a/newfile
+++ b/newfile
@@ -1,6 +1,9 @@
+Change supporting feature 1
 first change
 second change off branch
 third change off branch
 second change
 third change
 fourth change
+bug fix 1
+change supporting feature 1

我用分裂来回应,然后用“不”来应用第一个块。第二个大块头,我尝试编辑。我最初尝试删除底线 - 但没有成功。完全让这个大块头单独呆着也是行不通的,我不明白为什么。

I'm trying to use git add --interactive to selectively add some changes to my index, but I continually receive the "Your edited hunk does not apply. Edit again..." message. I get this message even if I choose the e option, and immediately save/close my editor. In other words, without editing the hunk at all, the patch doesn't apply.

Here's the exact example I'm using (I'm trying to put together a small demo):

Original file:

first change
second change off branch
third change off branch
second change
third change
fourth change

New file:

Change supporting feature 1
first change
second change off branch
third change off branch
second change
third change
fourth change
bug fix 1
change supporting feature 1

I'm trying to show how to use git add --interactive to only add the "bug fix 1" line to the index. Running interactive add on the file, I choose the patch mode. It presents me with

diff --git a/newfile b/newfile
index 6d501a3..8b81ae9 100644
--- a/newfile
+++ b/newfile
@@ -1,6 +1,9 @@
+Change supporting feature 1
 first change
 second change off branch
 third change off branch
 second change
 third change
 fourth change
+bug fix 1
+change supporting feature 1

I respond with split, followed by "no" to apply the first hunk. The second hunk, I try to edit. I originally tried deleting the bottom line - that didn't work. Leaving the hunk alone completely doesn't work either, and I can't figure out why.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

醉梦枕江山 2024-09-16 17:09:56

这是否像这篇 git-add 帖子中那样?

手动编辑大块非常强大,但如果您以前从未这样做过,也会有点复杂。
要记住的最重要的一点:除了存在其他缩进之外,差异始终缩进一个字符。
该字符可以是:

  • 一个空格(表示未更改的行),
  • - 表示该行已被删除,
  • + 表示该行已添加。

没有别的。它必须是空格、- 或 +。任何其他内容,您都会收到错误
(更改的行没有字符,因为这些是通过删除旧行并将更改的行添加为新行来处理的)。

既然你已经在你最喜欢的文本编辑器中打开了差异(你确实配置了 Git 使用你最喜欢的文本编辑器,对吧?),你可以做任何你想做的事情 - 只要你确保结果差异完全适用.

这就是窍门。如果您以前从未这样做过,Git 会告诉您“您编辑的块不适用。再次编辑吗?”很多时候,你会开始恨自己无法弄清楚这一点,尽管它看起来很容易(或者 Git,因为它无法弄清楚你想要什么)。

经常让我绊倒的一件事是我忘记了一个字符缩进。
我会用 - 标记要删除的行,但在大多数插入 - 的文本编辑器中,它不会覆盖之前的空格。 这意味着您要在整行中添加另一个空格,这又意味着 diff 算法无法在原始文件中找到/匹配该行,这又意味着 Git 会对您大喊大叫

另一件事是差异仍然必须有意义。 “感”是指可以干净地涂抹。确切地说,如何创建合理的差异似乎有点黑暗艺术(至少对我来说是这样),但你应该始终记住原始文件的样子,然后相应地规划你的 -s 和 +s。如果你经常编辑你的帅哥,你最终会掌握它的窍门。

另请参阅在 git add -p 上提交

Ortomala Lokni答案< /a> 引用 Joaquín Windmüller 博客文章“有选择地选择要使用 git 提交的更改(或者我要编辑你的块)

Git 想要做的不是计算行数,而是在应用所述编辑后的块之前合并重叠的块(当一个块被编辑时) .
这是在 2018 年中期讨论过的,避免出现以下情况:

如果您分割了一个大块,请编辑第一个子块,将
删除的尾随上下文行然后如果您尝试暂存
第二个子块,它会失败。


使用 Git 2.45(2024 年第 2 季度),第 16 批,“git add -p"(man) 和其他交互式块选择 UI 已经学会在块显示后立即跳过显示块,以及明确要求重新显示当前块的附加操作。

在您的情况下,在出现“您编辑的块不适用。再次编辑...”错误消息后,不适用的块将不会重复。

请参阅 提交 bab1f1c提交 66c14ab(2024 年 3 月 29 日),作者:Rubén Justo (rjusto
(由 Junio C Hamano -- gitster -- 合并于 提交 989bf45,2024 年 4 月 9 日)

add-patch:不打印帅哥反复

签字人:Rubén Justo

交互式补丁是一个连续的过程,在每个步骤中,我们都会从补丁中打印一个块,然后询问用户如何继续。

有可能重复某个步骤,例如,如果用户输入不适用的选项,即:“s

<前><代码>$ git add -p
diff --git a/add-patch.cb/add-patch.c
索引 52be1ddb15..8fb75e82e2 100644
---a/add-patch.c
+++ b/add-patch.c
@@ -1394,7 +1394,7 @@ N_("j - 让这个大块未定,看看下一个未定的大块\n"
静态 int patch_update_file(struct add_p_state *s,
结构 file_diff *file_diff)
{
- size_t hunk_index = 0;
+ size_t hunk_index = 0, prev_hunk_index = -1;
ssize_t i,未决定的_上一个,未决定的_下一个;
结构块*块;
字符ch;
(1/4) 上演这个大块头 [y,n,q,a,d,j,J,g,/,e,p,?]? s
抱歉,无法分割这个大块头
@@ -1394,7 +1394,7 @@ N_("j - 让这个大块未定,看看下一个未定的大块\n"
静态 int patch_update_file(struct add_p_state *s,
结构 file_diff *file_diff)
{
- size_t hunk_index = 0;
+ size_t hunk_index = 0, prev_hunk_index = -1;
ssize_t i,未决定的_上一个,未决定的_下一个;
结构块*块;
字符ch;
(1/4) 上演这个大块头 [y,n,q,a,d,j,J,g,/,e,p,?]?

...
或无效选项,即:“U

<前><代码>$ git add -p
diff --git a/add-patch.cb/add-patch.c
索引 52be1ddb15..8fb75e82e2 100644
---a/add-patch.c
+++ b/add-patch.c
@@ -1394,7 +1394,7 @@ N_("j - 让这个大块未定,看看下一个未定的大块\n"
静态 int patch_update_file(struct add_p_state *s,
结构 file_diff *file_diff)
{
- size_t hunk_index = 0;
+ size_t hunk_index = 0, prev_hunk_index = -1;
ssize_t i,未决定的_上一个,未决定的_下一个;
结构块*块;
字符ch;
(1/4) 上演这个大块头 [y,n,q,a,d,j,J,g,/,e,p,?]? U
y - 舞台这个大块头
n - 不要上演这个大块头
q-退出;不要上演这个大块头或任何剩余的块头
a - 将这个大块头和所有后来的大块头放入文件中
d - 不要在文件中暂存该块或任何后续块
j - 让这个大块头犹豫不决,看看下一个未决定的大块头
J - 让这个大块头犹豫不决,看看下一个大块头
g - 选择一个要前往的对象
/ - 搜索与给定正则表达式匹配的块
e - 手动编辑当前块
p - 再次打印当前块
? - 打印帮助
@@ -1394,7 +1394,7 @@ N_("j - 让这个大块未定,看看下一个未定的大块\n"
静态 int patch_update_file(struct add_p_state *s,
结构 file_diff *file_diff)
{
- size_t hunk_index = 0;
+ size_t hunk_index = 0, prev_hunk_index = -1;
ssize_t i,未决定的_上一个,未决定的_下一个;
结构块*块;
字符ch;
(1/4) 上演这个大块头 [y,n,q,a,d,j,J,g,/,e,p,?]?

在问题后再次打印该块可能会令人困惑,因为用户必须特别注意同一块正在被重新考虑。

如果块长于一个屏幕高度,也可能会出现问题,因为上一次迭代的结果会在屏幕外丢失(上一示例中的帮助指南)。

为了避免此类问题,如果迭代没有前进到不同的块,请停止打印该块。

但你也有:

add-patch:引入 'p'

签字人:Rubén Justo

很快我们就会让交互式补丁在某些情况下自动停止打印大块。

让我们引入一个新选项,允许用户明确请求打印。

git add 现在包含在其 手册页

p - 打印当前块

您可以随时使用 p 检查交互式补丁会话中的当前块。

Is this like in this git-add post?

Manually editing the hunk is immensely powerful, but also a bit complicated if you've never done it before.
The most important thing to keep in mind: The diff is always indented with one character in addition to whatever other indentation is there.
The character can either be:

  • a space (indicates an unchanged line),
  • a - indicating that the line was removed,
  • or a + indicating that the line was added.

Nothing else. It must be either a space, a - or a +. Anything else, and you'll get errors
(there's no character for a changed line, since those are handled by removing the old line, and adding the changed one as new).

Since you've got the diff open in your favorite text editor (you did configure Git to use your favorite text editor, right?), you can do whatever you want - as long as you make sure the resulting diff applies cleanly.

And therein lies the trick. If you've never done this before, Git will tell you "Your edited hunk does not apply. Edit again?" so often, you'll start to hate yourself for your inability to figure this out, even though it seems so easy (or Git because it can't figure out what you want).

One thing that tripped me up quite often was that I forgot the one character indent.
I'd mark a line with a - to be removed, but in most text editors that insert a -, it doesn't overwrite the space that was there before. This means you're adding another space to the whole line, which in turn means the diff algorithm can't find/match the line in the original file, which in turn means Git will yell at you.

The other thing is that the diff still has to make sense. "Sense" means that it can be applied cleanly. Exactly how you create a sensible diff seems to be a bit of an dark art (at least to me right now), but you should always keep in mind how the original file looked like, and then plan your -s and +s accordingly. If you edit your hunks often enough you'll eventually get the hang of it.

See also this commit on git add -p.

Ortomala Lokni's answer refers to Joaquín Windmüller blog post "Selectively select changes to commit with git (or Imma edit your hunk)"

Instead of counting lines, what Git would like to do is to coalesce overlapping hunks (when one is edited) before applying said edited hunk.
That was discussed mid-2018, and would avoid scenarios like:

if you split a hunk, edit the first subhunk, transforming a
trailing context line to a deletion then if you try to stage the
second subhunk, it will fail.


With Git 2.45 (Q2 2024), batch 16, "git add -p"(man) and other interactive hunk selection UI has learned to skip showing the hunk immediately after it has already been shown, and an additional action to explicitly ask to reshow the current hunk.

In your case, the hunk which does not apply will not be repeated after a "Your edited hunk does not apply. Edit again..." error message.

See commit bab1f1c, commit 66c14ab (29 Mar 2024) by Rubén Justo (rjusto).
(Merged by Junio C Hamano -- gitster -- in commit 989bf45, 09 Apr 2024)

add-patch: do not print hunks repeatedly

Signed-off-by: Rubén Justo

The interactive-patch is a sequential process where, on each step, we print one hunk from a patch and then ask the user how to proceed.

There is a possibility of repeating a step, for example if the user enters a non-applicable option, i.e: "s"

$ git add -p
diff --git a/add-patch.c b/add-patch.c
index 52be1ddb15..8fb75e82e2 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -1394,7 +1394,7 @@ N_("j - leave this hunk undecided, see next undecided hunk\n"
 static int patch_update_file(struct add_p_state *s,
               struct file_diff *file_diff)
 {
- size_t hunk_index = 0;
+ size_t hunk_index = 0, prev_hunk_index = -1;
  ssize_t i, undecided_previous, undecided_next;
  struct hunk *hunk;
  char ch;
(1/4) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? s
Sorry, cannot split this hunk
@@ -1394,7 +1394,7 @@ N_("j - leave this hunk undecided, see next undecided hunk\n"
 static int patch_update_file(struct add_p_state *s,
               struct file_diff *file_diff)
 {
- size_t hunk_index = 0;
+ size_t hunk_index = 0, prev_hunk_index = -1;
  ssize_t i, undecided_previous, undecided_next;
  struct hunk *hunk;
  char ch;
(1/4) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?

...
or an invalid option, i.e: "U"

$ git add -p
diff --git a/add-patch.c b/add-patch.c
index 52be1ddb15..8fb75e82e2 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -1394,7 +1394,7 @@ N_("j - leave this hunk undecided, see next undecided hunk\n"
 static int patch_update_file(struct add_p_state *s,
               struct file_diff *file_diff)
 {
- size_t hunk_index = 0;
+ size_t hunk_index = 0, prev_hunk_index = -1;
  ssize_t i, undecided_previous, undecided_next;
  struct hunk *hunk;
  char ch;
(1/4) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? U
y - stage this hunk
n - do not stage this hunk
q - quit; do not stage this hunk or any of the remaining ones
a - stage this hunk and all later hunks in the file
d - do not stage this hunk or any of the later hunks in the file
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
g - select a hunk to go to
/ - search for a hunk matching the given regex
e - manually edit the current hunk
p - print again the current hunk
? - print help
@@ -1394,7 +1394,7 @@ N_("j - leave this hunk undecided, see next undecided hunk\n"
 static int patch_update_file(struct add_p_state *s,
               struct file_diff *file_diff)
 {
- size_t hunk_index = 0;
+ size_t hunk_index = 0, prev_hunk_index = -1;
  ssize_t i, undecided_previous, undecided_next;
  struct hunk *hunk;
  char ch;
(1/4) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?

Printing the chunk again followed by the question can be confusing as the user has to pay special attention to notice that the same chunk is being reconsidered.

It can also be problematic if the chunk is longer than one screen height because the result of the previous iteration is lost off the screen (the help guide in the previous example).

To avoid such problems, stop printing the chunk if the iteration does not advance to a different chunk.

But you also have:

add-patch: introduce 'p' in interactive-patch

Signed-off-by: Rubén Justo

Shortly we're going make interactive-patch stop printing automatically the hunk under certain circumstances.

Let's introduce a new option to allow the user to explicitly request the printing.

git add now includes in its man page:

p - print the current hunk

You can inspect at any time the current hunk in your interractive patch session with p.

美胚控场 2024-09-16 17:09:56

当然,我迟到了,但还是想记录一下这个问题 去年在 git 邮件列表上进行了讨论,从那以后看起来没有太大变化。

这个特殊问题源于分裂尝试编辑同一个块。正如 Jeff King 最初发布的那样,对根本问题的分析基本上是:

嗯。好的,我明白了。 “此差异是否适用”检查提要两个部分
git-apply 的分割补丁。但当然第二部分永远不会
正确应用,因为它的上下文与第一部分重叠,但是
没有考虑到这一点。

仅使用编辑后的补丁进行检查就可以了。但那个
没有考虑到您编辑的补丁可能会失败
从长远来看,取决于您是否接受
分割补丁的另一半。我们还不知道这一点,因为
用户可能没有告诉我们(他们可以跳过前半部分,并且
然后在编辑步骤之后再回来查看)。

Jeff 以一种非常务实的解决方法结束了他的文章,该解决方法总是成功,因此强烈推荐:

所以总的来说,我认为分割和编辑同一个块本质上是
危险并且会导致此类问题。并且因为
编辑提供了功能的超集,我认为你应该
只需编辑并允许应用大块的第一部分或
不取决于你的喜好。

通过仅选择编辑先前未分割的块,您将不必处理行号。

Of course I'm late to this but nevertheless wanted to mention for the record that this issue was discussed last year on the git mailing list and it looks like not much has changed since.

This particular issue stems from splitting and attempting to edit the same hunk. The analysis, as originally posted by Jeff King, of the underlying problem essentially is:

Hm. OK, I see. The "does this diff apply" check feeds both parts of
the split patch to git-apply. But of course the second part will never
correctly apply, because its context overlaps with the first part, but
doesn't take it into account.

Doing the check with just the edited patch would work. But that
doesn't take into account that your edited patch will potentially fail
to apply in the long run, depending on whether or not you accept the
other half of the split patch. And we can't know that yet, because the
user may not have told us (they could have skipped the first half, and
then come back to it later after the edit step).

Jeff concludes his post with a very pragmatic workaround that always succeeds, and is thus highly recommended:

So in general, I think splitting and editing the same hunk is inherently
dangerous and is going to lead to these sorts of problems. And because
editing provides a superset of the functionality, I think you should
just edit and either allow the first part of the hunk to be applied or
not depending on your preference.

By only choosing to edit a hunk not previously split, you will not have to deal with the line numbers.

晨曦慕雪 2024-09-16 17:09:56

对于这个特定的示例,您需要调整块中的行号。更改行:

@@ -1,6 +2,8 @@

,使其改为:

@@ -2,7 +2,8 @@

For this particular example, you need to tweak the line numbers in the hunk. Change the line:

@@ -1,6 +2,8 @@

so that it instead reads:

@@ -2,7 +2,8 @@
走野 2024-09-16 17:09:56

正确修改 hunk 标头(例如 @@ -1,6 +1,9 @@)也很重要。 Joaquin Windmuller 在他的一篇 博客文章

编辑帅哥的秘密

编辑 hunks 一开始可能会令人困惑,git 的说明
为您提供帮助,但还不足以开始。

<前><代码># —||

# 要删除 '-' 行,请将它们设为 ' ' 行(上下文)。

# 要删除“+”行,请将其删除。

# 以 # 开头的行将被删除。

#

# 如果补丁应用干净,编辑后的块将立即被

# 标记为暂存。如果应用不干净,您将被给予

# 再次编辑的机会。如果把大块头的所有线条都去掉,

# 然后编辑被中止,块保持不变。

秘诀是……计算行数:

  • 如果删除以 + 开头的行,则在新行数中减去 1(该块标题的最后一位数字)
  • 如果删除以 - 开头的行,则在新行数中添加 1(该块标题的最后一位数字)
  • 请勿删除其他线(参考线)。

这应该允许您快速修改帅哥以选择零件
你想要的。

It's important to also correctly modify the hunk header (e.g. @@ -1,6 +1,9 @@). Joaquin Windmuller reveal the secret of hunk header editing in one of his blog post.

The secrets of editing hunks

Editing the hunks can be confusing at first, the instructions that git
gives you help but are not enough to get started.

# —||

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

The secret sauce is…counting lines:

  • If you remove a line that starts with + then subtract one to the new line count (last digit of the hunk’s header).
  • If you remove a line that starts with - then add one to the new line count (last digit of the hunk’s header).
  • Don’t remove the other lines (reference lines).

This should allow you to quickly modify the hunks to select the parts
you want.

喵星人汪星人 2024-09-16 17:09:56

当您不想删除暂存待删除的行时,例如

 first line
-second line
 third line

您想要保留第二行的位置,请确保将 - 替换为空格,而不是删除整行(如您会删除添加的行)。 Git 将使用该行作为上下文。

When you want to not delete a line that staged for deletion, as in

 first line
-second line
 third line

where you want to keep the second line, make sure you replace the - with a space, rather than deleting the whole line (as you would to get rid of an added line). Git will use the line for context.

如果没有你 2024-09-16 17:09:56

我最近通过阅读这篇文章弄清楚了如何进行手动编辑。

我使用的技巧是,如果我有这样的差异:

+ Line to add
+ Line to add
+ Line I dont want to include
+ Line I dont want to include

技巧是完全删除我不想要的两行,使生成的差异看起来像:

+ Line to add
+ Line to add

虽然这对大多数人来说很可能是显而易见的,但它不适合我直到今天,我想我应该分享我的经验。请告诉我这种方法是否有任何危险。

I recently figured out from reading this thread how to do the manual editing.

The trick I used was that if I have a diff like:

+ Line to add
+ Line to add
+ Line I dont want to include
+ Line I dont want to include

The trick is to completely remove the two lines I don't want, making the resulting diff looking like:

+ Line to add
+ Line to add

While this is most likely obvious to most people, it was not for me until today and I thought I should just share my experience. Please tell me if there is any danger to this method.

樱花细雨 2024-09-16 17:09:56

您可以手动编辑行号,这在某些情况下绝对有用。但是,您可能可以通过不首先拆分大块来避免这个特定问题。

如果您发现稍后可能需要在 Git 自动选择的块中编辑某些内容,那么最好只编辑整个块,而不是拆分、暂存一半,然后编辑另一半。 Git 会更好地解决这个问题。

You can manually edit the line numbers, that's definitely useful in some cases. However, you probably could have avoided this particular problem by NOT first splitting the hunk.

If you see that you'll probably need to edit something later in the hunk that Git has automatically chosen, it's best to just edit the whole hunk rather than splitting, staging half, and then editing the other half. Git will do a better job figuring that out.

信仰 2024-09-16 17:09:56

我来到这个问题寻找相同问题的解决方案,并且无法弄清楚如何更改大块中的行号(如上所述)以使 git 在我的情况下接受它。不过,我找到了一种更好的方法来使用 git gui 来做到这一点。在那里,您可以选择差异中要暂存的行,然后右键单击并选择“从提交暂存行”。我记得 git-cola 也有相同的功能。

I came to this question looking for solution to same problem, and couldn't figure out how to change the line numbers (as suggested above) in the hunk to get git accept it in my case. I found a much better way to do that using git gui though. There you can select the lines in the diff that you want to stage, then right click and choose "Stage lines from commit". I remember git-cola has the same functionality too.

时光匆匆的小流年 2024-09-16 17:09:56

奇怪的“你编辑的块不适用”消息(可能伴随着类似“错误:补丁片段在行中没有标题”)的原因之一可能是你的编辑器,如果它配置为去除尾随空格。这显然会导致重大问题,因为补丁将空行编码为带有一个空格的行,如果使用这样的编辑器保存,任何包含空行的块都将无法应用。因此,实际上,如果启用了剥离尾随空格,则任何包含任何未更改的空行的块在编辑后都将无法应用。

One reason for weird "Your edited hunk does not apply" messages (likely accompanied with something like "error: patch fragment without header at line...") might be your editor if it's configured to strip trailing whitespace. This would obviously cause major problems as patches encode empty lines as lines with one space any hunk containing empty lines would fail to apply if saved with such an editor. So in effect any hunk containing any unchanged empty lines would fail to apply after editing with if stripping trailing whitespace is on.

预谋 2024-09-16 17:09:56

当我收到此错误时遇到的另一个问题是,当我保存编辑文件时,行结尾发生了变化。

我使用 Windows 并使用记事本进行编辑(仅使用 Windows 行结尾保存)。我的代码是用 Notepad++ 编写的,我将其设置为具有 Unix/Linux 样式的行结尾。

当我更改设置以将 Notepad++ 作为默认 git 编辑器时,我可以对这个块进行编辑。

git config --global core.editor "notepad++"

An additional problem I had when I got this error was that the line endings changed when I saved the edit file.

I was using Windows and using Notepad for my edits (only saves with Windows line-endings). My code was written with Notepad++ and I set it up to have Unix/Linux style line endings.

When I changed my settings to have Notepad++ as the default git editor I was able to make my edits to the hunk.

git config --global core.editor "notepad++"
最丧也最甜 2024-09-16 17:09:56

一般来说,问题可能是补丁未正确完成,或者是我将在帖子底部描述的更具体的问题,

如何在 VIM 中编辑

如果< code>vim 是编辑器,将光标放在行中的第一个字符上,然后执行以下操作(假设您处于默认的命令模式或按 esc )基于当前字符和您想要执行的操作:

  1. 对于以“”空格开头的行,表示未更改:

    • 如果您想删除它,请输入 r-
  2. 对于以“<”开头的行code>-' 表示它将被删除(或替换):

    • 如果您不想删除它,请输入 r(即先按 r,然后按空格)
    • 注意行的顺序,见下文
  3. 对于带有 '+' 的星号行,这意味着它将被添加(或替换 '-' 之前的行):

    • 如果您不想添加它,请输入dd

处理行顺序问题

另外,您可能希望移动各行,以便 + 行位于 之后- 行,否则 + 添加的行将移动到未更改的行之后,如下例所示:

假设您在编辑之前有以下补丁:

-Line1
-Line2
+NewLine1
+NewLine2

并且您只想暂存第一行,请勿执行以下操作:

-Line1
Line2
+NewLine1

因为这会导致 NewLine1Line2 之后结束! (除非这是您想要的),而是使用 此处描述的 VIM 命令

保存

完成后,只需输入 :wq 就可以了。

错误的另一个原因

如果在执行补丁 git add 时也从外部修改了更改,您也可能会收到此错误消息(即使您正确执行了所有操作)。

(例如,您使用另一个终端或 GUI 来暂存更改。)

这会导致实际尝试​​应用更改时行号失败。

在这种情况下,您应该先退出(通过使用“q”等),然后重新启动。

The issue might be in general that the patch wasn't done correctly or a more specific issue that I will describe in the bottom of the post,

How to Edit in VIM

The way you should do it if vim is the editor, is by placing the cursor over the first character in the line, and do the following (assuming you are in command mode which is the default or after pressing esc) based on the current character and what you want to do:

  1. For a line that starts with a '' space which means unchanged:

    • If you want to DELETE it then type r-
  2. For a line that starts with a '-' which means it will be removed (or replaced):

    • If you do NOT want to delete it then type r (that's r and then space)
    • Be careful with the order of the lines, see below
  3. For a line that was stars with a '+' which means it will be add (or replace the '-' line before):

    • If you do NOT want to add it then type dd

Handling the line order issue

Also you would probably want to move around lines so that the + line is right after the - line otherwise the + added line will be moved after the unchanged lines, as in this example:

Say you have the following patch before editing:

-Line1
-Line2
+NewLine1
+NewLine2

And you want to only stage the first line, the DO NOT do the following:

-Line1
Line2
+NewLine1

as it will cause NewLine1 to end up AFTER Line2! (unless this is what you want), instead move around the lines using the VIM commands described here

Saving

Once you are done just type :wq and you should be fine.

Another cause of the error

You can also get this error message (even if you did everything correctly) if while going through the patches git add the changes were also modified externally.

(For example you used another terminal or GUI to stage changes.)

This causes the line numbers to fail when actually trying to apply the changes.

In this case you should rather quit first (by using 'q' etc.) and then restart.

无法言说的痛 2024-09-16 17:09:56

我曾经有过这个问题。如果使用 VIM 在 Windows 中进行交互式添加,则不必调整块标头(@@ 之间的内容),您所要做的就是将文件行结尾设置为 Unix (LF)。

在 VIM 编辑器中,只需在写入/退出之前执行以下命令:

:set ff=unix

I used to have this problem. If doing an interactive add in windows, using VIM, you don't have to tweak the hunk header (the stuff between the @@'s), all you have to do is set the file line endings to Unix (LF).

In the VIM editor, simply do the following command sometime before your write/quit:

:set ff=unix
撩心不撩汉 2024-09-16 17:09:56

仅供参考,我遇到了一个稍微相关的错误...当我按照上面建议的说明添加修补程序时...但是,它没有显示任何错误。我不断收到它要求我上演同样的大块...我注意到我正在运行旧版本的 Vim 7.4...我升级了 vim,它现在按预期工作。希望这会帮助某人..

FYI, I was getting a slightly interrelated error... when I add patched following the suggested instruction above... It was showing no error, however. I was getting it repeatedly asking me to stage the same hunk... I noticed I was running an older version of Vim 7.4... I upgraded vim and it's working as expected now. Hopefully this will help someone..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文