38

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 exception when I tried above command:

Error:

~/Documents$ git cherry-pick stash@{0}
error: Commit 4590085c1a0d90de897633990f00a14b04405350 is a merge but no -m option was given.
fatal: cherry-pick failed
would_like_to_be_anon
  • 1,639
  • 2
  • 29
  • 47
  • What do you expect this to do? To git, `cherry-pick` means very specifically applying the changes in just one commit from somewhere (i.e. a different branch), in isolation, to the current branch. If you want to do that for a stash you just `apply` the stash (or `pop` it if you don't need the stash anymore and want to also remove it). – rakslice Jul 31 '20 at 22:02

3 Answers3

47

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.

enter image description here

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
Klas Mellbourn
  • 42,571
  • 24
  • 140
  • 158
  • 2
    >>I am not sure exactly what you want to achieve by cherry-picking? -- In my case, a simple merge would be fine by doing stash apply/pop. But, I was wondering if I can cherry-pick few changes from the stashed changes. Thanks for your reply. – would_like_to_be_anon May 31 '13 at 20:07
  • Is it possible to do this "git stash branch stashchanges" to pick a specific item (such as #5 in the stash) -- such as something like "git stash@{5} branch stashchanges" ? – mochsner Sep 01 '21 at 16:33
  • 1
    Ah, looks like you can do this via a specific stash commit via `git stash branch stashchanges stash@{1}` – mochsner Sep 01 '21 at 17:18
4

This works for me... git cherry-pick -nm1 stash

YMMV

scaly
  • 509
  • 8
  • 18
-6

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]

Bhaskar
  • 2,549
  • 1
  • 21
  • 23