Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cherry-pick from stash in git?

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 
like image 381
would_like_to_be_anon Avatar asked May 31 '13 16:05

would_like_to_be_anon


People also ask

How do I pull files from a git stash?

To retrieve changes out of the stash and apply them to the current branch you're on, you have two options: git stash apply STASH-NAME applies the changes and leaves a copy in the stash. git stash pop STASH-NAME applies the changes and removes the files from the stash.

What is git stash and git cherry pick?

Cherry-picking is the process of picking a commit from a branch and applying it to another branch. git cherry-pick can be useful for undoing changes. It is useful when you commit accidentally to the wrong branch. You can switch to the correct branch and cherry-pick the commit to where it should belong to.


1 Answers

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 
like image 143
Klas Mellbourn Avatar answered Sep 19 '22 16:09

Klas Mellbourn