git rebase:奇怪的隐藏行为

git rebase: weird stash behaviour

我做了一个 git rebase -i,选择了一个提交进行编辑,git 在那个提交处停止了。

我现在尝试从 stash checkout 一个补丁 git checkout -p stash.

但这给了我奇怪的旧存储内容,而 git stash show -p 给了我预期的补丁,我想从中检出一个补丁。

这里发生了什么,我怎样才能从藏品中取出我的大块头?
如果我要提供更多信息,请告诉我。

首先,我们来看一下git checkout的文档。

git checkout [-p|--patch] [] [--] ...

When <paths> or --patch are given, git checkout does not switch branches. It updates the named paths in the working tree from the index file or from a named <tree-ish> (most often a commit). In this case, the -b and --track options are meaningless and giving either of them results in an error. The <tree-ish> argument can be used to specify a specific tree-ish (i.e. commit, tag or tree) to update the index for the given paths before updating the working tree.

git checkout with <paths> or --patch is used to restore modified or deleted paths to their original contents from the index or replace paths with the contents from a named <tree-ish> (most often a commit-ish).

正如我们所见,-p 之后的参数应该是一个特定的树状结构(即提交、标记或树),而您通过了 stash。那么,git如何处理stash才是关键。

您可以尝试 git checkout stash 以查看 git 将转到哪个提交,这就是您实际 checkout -p.

最后,我想你可以使用 git stash apply 让它工作。

为什么不使用 checkout 命令直接使用 stash?


git stash pop

Remove a single stashed state from the stash list and apply it on top of the current working tree stat


git stash apply

Like pop, but do not remove the state from the stash list


git stash show

Show the changes recorded in the stash as a diff between the stashed state and its original parent


在您的情况下,您需要使用 stash@{<revision>} 来执行您希望执行的任何隐藏操作。

例如:

git stash show -p stash@{0}

查看隐藏内容

# display on the stash content
git stash show stash@{1} -u

# checkout the desired commit and choose the right action you want
git checkout -p stash@{0} -p <path to file>