gpt4 book ai didi

interactive - git add --interactive "Your edited hunk does not apply"

转载 作者:行者123 更新时间:2023-12-03 05:12:42 26 4
gpt4 key购买 nike

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

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

原始文件:

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

我用 split 来回应,然后用“no”来应用第一个 block 。第二个大块头,我尝试编辑。我最初尝试删除底线 - 但没有成功。完全不理睬这个大块头也行不通,我不明白为什么。

最佳答案

就像 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 inserts a -, it doesn't overwrite the space that was there before. This means you're adding an additional 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.

另请参阅 commit on git add -p .

Ortomala LoknianswerJoaquín Windmüller 博客文章“ Selectively select changes to commit with git (or Imma edit your hunk)

Git 想要做的不是计算行数,而是在应用所述编辑后的 block 之前合并重叠的 block (当编辑 block 时)。
那是discussed mid-2018 ,并且会避免这样的情况:

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.

关于interactive - git add --interactive "Your edited hunk does not apply",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3268596/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com