How to cherry-pick from stash in git?

不羁岁月 提交于 2019-11-30 08:21:05

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-untracked flag) any untracked files in a third commit.

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

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

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

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

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

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.

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

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

   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]

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!