如何从 git 中的存储中挑选?

问题描述 投票:0回答:3

我想知道是否可以从储藏室中挑选樱桃。

git stash save "test cherry-pick from stash"

git cherry-pick stash@{0} # --> Is this possible?

当我尝试上述命令时,出现以下错误:

~/Documents$ git cherry-pick stash@{0}
error: Commit 4590085c1a0d90de897633990f00a14b04405350 is a merge but no -m option was given.
fatal: cherry-pick failed
git cherry-pick
3个回答
48
投票

问题是一个存储由两个或三个提交组成。存储时,修改后的工作树存储在一次提交中,索引存储在一次提交中,(如果使用

--include-untracked
标志)任何未跟踪的文件存储在第三次提交中。

如果您使用

gitk --all
并进行隐藏,您就可以看到这一点。

enter image description here

stash@{0}
指向包含工作树的提交。

但是,如果您这样做,您可以从该提交中进行挑选

git cherry-pick "stash@{0}" -m 1

cherry-pick
认为存储是合并,因此需要
-m 1
参数的原因是存储提交有多个父级,如图所示。

我不确定你到底想通过挑选来实现什么。一个可能的替代方案是从存储中创建一个分支。在那里提交更改并将它们合并到您当前的分支。

git stash branch stashchanges
git commit -a -m "changes that were stashed"
git checkout master
git merge stashchanges

4
投票

这对我有用...... gitcherry-pick-nm1 stash

YMMV


-7
投票

我以前没有这样做过。但是cherry-pick 的手册页说它只适用于提交。

   Given one or more existing commits, apply the change each one introduces,
   recording a new commit for each. This requires your working tree to be
   clean (no modifications from the HEAD commit).

隐藏不是提交,也不会移动 HEAD。所以,这是不可能的[但这只是一个猜测]

© www.soinside.com 2019 - 2024. All rights reserved.