17

How can I specify default merge strategy when I do a git stash pop?

I tried

git stash pop --theirs

but that doesn't work.

John Henckel
  • 10,274
  • 3
  • 79
  • 79

3 Answers3

14

This is now possible:

git cherry-pick -n -m1 -Xtheirs stash

The literal string stash now represents the top stash entry (you can also do stash@{1} for the one below that, and so on.

More details on how this works are on this answer.

tmandry
  • 1,295
  • 16
  • 33
8

Side note: you do not want --theirs—that's an option to git checkout, not to merges—and there is no -s theirs strategy, there is only a -X theirs strategy option (I like to call these "extended options" to distinguish them from -s strategies).

The answer, however, is that you can't: it is simply not supported as part of the git stash code.

It is possible to do it a different way. The git stash script, which is a shell script you can copy and modify or run its various bits piecemeal, runs git merge-recursive $b_tree -- $c_tree $w_tree to merge in the stashed work-tree commit. You could do this yourself, manually or by copying and modifying the script, with additional -X extended-options. This is, however, not guaranteed to do what you want. It is only going to affect parts that Git thinks are conflicting, in which case it will favor one side or the other: -X ours means favor the $b_tree-to-$c_tree change, instead of the $b_tree to $w_tree change, and -X theirs means favor the $b_tree to $w_tree change. You may well want the entire file from $w_tree commit, though, or not to take some change(s) that nevertheless don't conflict.

(It would be easier and more straightforward to make your own commit, which you could make on a private branch; you can then extract individual files, and/or do whatever merges you like, from that commit at any time, and not have to worry about particular internal details of the git stash script that might change from one Git version to another. Note that to merge one particular file at a time, you can use git merge-file, but it's kind of klunky.)

Community
  • 1
  • 1
torek
  • 448,244
  • 59
  • 642
  • 775
  • This is not especially good news. So instead of remembering it all I'll probably go for painstakingly: create a [temporary] branch instead of the stash -> checkout target to be updated with the [not anymore] stashed changes -> merge branches where the strategy is available. – Tomasz Gandor May 16 '18 at 06:22
  • 1
    @TomaszGandor: for anything beyond simple stash-checkout-unstash, I recommend making commits on temporary branches anyway. It's way too easy to build up a bunch of anonymous stashes otherwise, and when you come back to them later, there's often no clue what you were doing. Maybe some people can remember, but I confuse myself :-) – torek May 16 '18 at 14:52
3

So if you arrived here after a git stash pop with conflicts, looking like this:

$ git stash pop
Auto-merging foo.json
CONFLICT (content): Merge conflict in bar.json

and you'd rather just discard what you had stashed in bar.json, the easiest way is to checkout ours:

$ git checkout --ours bar.json

The stuff coming out of the stash is considered "theirs." So if you want to discard it, check out "ours."

Matt Montag
  • 7,105
  • 8
  • 41
  • 47