git stash 和编辑帅哥
我非常喜欢 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 (usinge
) because the splitting that git suggests does not suit megit stash --keep-index
: then I do some testing, and if the tests pass I do not commitgit stash pop
: now the problem occurs: the filemy_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 rungit add my_file
followed bygit 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要创建和测试包含部分工作树更改(包括手动编辑的块)的索引,请执行以下操作:
此时不存在冲突,并且存储库、索引和工作目录处于紧邻 git stash 之前的状态。您现在可以
git commit
索引的更改。当然,这很奇怪而且不太直观,我真的很想知道是否有更简单的方法来做到这一点。
To create and test an index containing part of the working tree changes, including manually edited hunks, do:
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 nowgit 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.
我在 git 邮件列表中提出了这个问题。我所描述的是预期的行为。这不是一个错误。 :-(
这是我得到的答案:
对我来说,这是 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:
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.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.