如何从 git 中的 stash 中挑选?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16861982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 08:34:53  来源:igfitidea点击:

How to cherry-pick from stash in git?

gitcherry-pick

提问by would_like_to_be_anon

I am wondering if cherry-picking from stash is possible.

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

git stash save "test cherry-pick from stash"

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

I am getting the following exceptionwhen I tried above command:

我在exception上面尝试时得到以下信息command

Error:

Error

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

回答by Klas Mellbourn

The problem is that a stash consists of two or three commits. When stashing, the modified working tree is stored in one commit, the index in one commit, and (if using the --include-untrackedflag) any untracked files in a third commit.

问题是一个存储包含两个或三个提交。存储时,修改后的工作树存储在一次提交中,索引存储在一次提交中,以及(如果使用--include-untracked标志)任何未跟踪的文件在第三次提交中。

You can see this if you use gitk --alland do a stash.

如果您使用gitk --all并进行存储,您可以看到这一点。

enter image description here

在此处输入图片说明

stash@{0}points to the commit that contains the working tree.

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

You can however cherry-pick from that commit if you do

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

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

The reason that cherry-pickthinks that the stash is a merge, and thus needs the -m 1parameter is that the stash commit has multpile parents, as you can see in the graph.

其原因cherry-pick认为,藏匿的合并,因此需要的-m 1参数是藏匿犯有multpile父母,你可以在图中看到。

I am not sure exactly what you want to achieve by cherry-picking. A possible alternative is to create a branch from the stash. Commit changes there and merge them to your current branch.

我不确定你想通过挑选樱桃达到什么目的。一种可能的替代方法是从 stash 创建一个分支。在那里提交更改并将它们合并到您当前的分支。

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

回答by Bhaskar

I have not done this before. But the man-page on cherry-pick says that it works on commits only.

我以前没有这样做过。但是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).

Stashing is not a commit and doesn't move HEAD. So, this cannot be done [this is only a guess though]

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