how to specify default merge strategy on git stash pop
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.
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.)
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."