Skip to contentSkip to conversation

Behavior of stash apply vs merge

DISCUSSION Robin Rosenberg 2 participants 5 messages Jun 15, 2016 1 message not in the archive

lore
Share & tools
lore
#2 lore raw
Quoted 2 lines · Robin Rosenberg

What good reason is it that 'git stash apply' gives hairy conflict markers, while 'git merge stash' does not. No renames involved.

"git merge stash" is nonsensical and would do a vastly different thing compared to "git stash apply" depending on where you created the stash and where you are attempting to run that operation.

Imagine you started fixing a bug while on the 'master' branch, realized that the fix equally well applies to your 'maint' branch. You would do "git stash" followed by "git checkout maint".

A sane person would do "git stash apply" at this point. It applies the difference between the 'master' you were working on and your WIP on top of your 'maint'.

"git merge stash" is entirely different. The history leading to a stash looks like this:

                   I
                  / \
      ---o---o---B---W

where

	B is the commit you were working on (i.e. 'master');
	I records the state of the index;
	W records the state of the working tree.

and "stash" refers to W.

Think what commit B is in this example and the reason why you should never ever do "git merge stash" will become apparent. By merging W into 'maint', you would be pulling the entire history between 'maint' and 'master' to the result.

#3 lore raw

Thanks. Feeling a bit studid now.

I was actually thinking about using merge to implement stash apply in JGit. What we have is broken so I tried using merge to implement it and them compared to git merge --no-commit.. FAIL.

The main difference is of course that I set the merge base to stash^1, which is obviously not what my question was about.

-- robin

#4 lore raw
Quoted 5 lines · Robin Rosenberg

Thanks. Feeling a bit studid now.

I was actually thinking about using merge to implement stash apply in JGit. What we have is broken so I tried using merge to implement it and them compared to git merge --no-commit.. FAIL.

Do you have "cherry-pick"?

In short, "stash apply" is a "cherry-pick" in disguise.

#5 lore raw

----- Ursprungligt meddelande -----

Quoted 11 lines
Quoted 5 lines · Robin Rosenberg

Thanks. Feeling a bit studid now.

I was actually thinking about using merge to implement stash apply in JGit. What we have is broken so I tried using merge to implement it and them compared to git merge --no-commit.. FAIL.

Do you have "cherry-pick"?

In short, "stash apply" is a "cherry-pick" in disguise.

Yes, that's what I did. Thanks for confirming this. One for the working tree and if that succeeds I do another one to restore the index if requested.

-- robin