git stash 和编辑帅哥

发布于 2024-09-30 01:54:21 字数 1188 浏览 7 评论 0原文

我非常喜欢 git add -p 和 git stash ,但我偶尔会遇到以下问题,该问题是通过以下命令序列重现的:

  • git add -p my_file :然后我手动编辑一个块(使用e),因为 git 建议的拆分不适合我
  • git stash --keep-index< /code>:然后我做了一些测试,如果测试通过我不提交
  • git stash pop:现在出现问题:文件my_file 现在被认为是冲突的,并且 git 完全搞乱了我编辑的大块,所以我必须编辑该文件,删除无用的合并标记,然后运行 ​​git add my_file 后跟 git reset HEAD

我很困惑,因为只有在手动编辑块时才会发生这种情况。我完全不明白这会有什么不同。


要重现该问题:

  • touch newfile
  • git add newfile
  • git commit -m 'newfile'
  • 在文件中添加两行
  • git add - p newfile
  • 编辑 hunk (e),删除 hunk 中的一行,然后退出 git add (q)
  • git stash -- keep-index
  • git stash pop

现在文件 newfile 处于未合并状态。再次请注意,该问题仅出现在手动编辑的帅哥中。如果不手动编辑任何块,上述命令没有任何问题。

顺便说一下,文件的先前状态处于第三阶段(git show :3:newfile),先前暂存的版本处于第二阶段(git show :2:newfile< /代码>)。因此,我可以通过一些 git 黑魔法,设法将第二阶段放入该索引中,并将第三阶段放入工作存储库中......但我不知道该怎么做,所以我手动完成。 :-(

I totally love git add -p and git stash but I occasionally have the following problem, which is reproduced by the following sequence of commands:

  • git add -p my_file: then I edit a hunk manually (using e) because the splitting that git suggests does not suit me
  • git stash --keep-index: then I do some testing, and if the tests pass I do not commit
  • git stash pop: now the problem occurs: the file my_file is now considered as conflicted, and git has completely messed with my edited hunk, so I have to edit the file, remove the useless merge marks, and run git add my_file followed by git reset HEAD

I'm puzzled because this happens only when editing a hunk manually. I don't see how this should make any difference at all.


To reproduce the problem:

  • touch newfile
  • git add newfile
  • git commit -m 'newfile'
  • add two lines in the file
  • git add -p newfile
  • edit the hunk (e), remove one of the line in the hunk, then quit git add (q)
  • git stash --keep-index
  • git stash pop

Now the file newfile is in unmerged state. Note, again, that the problem only occurs with manually edited hunks. There is no problem whatsoever with the commands above if one does not edit any hunk manually.

Incidentally, the preceding state of the file is in the third stage (git show :3:newfile), and the previously staged version is in the second stage (git show :2:newfile). So I could, by some git black magic, manage to put the second stage in this index, and the third stage in the working repo... but I don't know how to do that so I do it by hand. :-(

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

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

发布评论

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

评论(3

红焚 2024-10-07 01:54:21

要创建和测试包含部分工作树更改(包括手动编辑的块)的索引,请执行以下操作:

git add --patch <files>

git stash --keep-index

<test the indexed changes>

git reset --hard

git stash pop --index

此时不存在冲突,并且存储库、索引和工作目录处于紧邻 git stash 之前的状态。您现在可以git commit索引的更改。

当然,这很奇怪而且不太直观,我真的很想知道是否有更简单的方法来做到这一点。

To create and test an index containing part of the working tree changes, including manually edited hunks, do:

git add --patch <files>

git stash --keep-index

<test the indexed changes>

git reset --hard

git stash pop --index

At this point there are no conflicts, and the repository, index, and working directory are in the state immediately preceding the git stash. You can now git commit the indexed changes.

Of course, this is rather weird and not very intuitive and I would really like to know whether there is an easier way to do this.

空城之時有危險 2024-10-07 01:54:21

我在 git 邮件列表中提出了这个问题。我所描述的是预期的行为。这不是一个错误。 :-(

这是我得到的答案:

如果您没有手动编辑该块,则每个块将位于
状态 HEAD 或状态 A,并将 HEAD 和 A 之间的差异应用于
这样的文件要么是无操作(块已经应用),要么是
申请成功。

对我来说,这是 git add --patch 的一个严重限制,我不明白这种行为对任何人都有什么用处,但我会学会忍受它。

I asked the question in the git mailing list. What I describe is the expected behaviour. It's not a bug. :-(

Here is the answer I got:

If you did not edit the hunk manually, each hunk will be either in
state HEAD or in state A, and applying the diff between HEAD and A to
such file will be either a no-op (hunk already applied), or a
successfull application.

For me this is a severe limitation of git add --patch, and I don't understand in what way this behaviour may be useful to anyone, but I will learn to live with it.

堇色安年 2024-10-07 01:54:21

git stash --keep-index 保留您的索引,但它仍然将索引内容添加为存储的一部分。

尝试 git stash save -p - 保存存储有点乏味,但可能会做你想做的事。

git stash --keep-index preserves your index, but it still adds the index contents as part of the stash.

Try git stash save -p -- a bit more tedious to save the stash, but will probably do what you want.

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