Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to specify default merge strategy on git stash pop

Tags:

git

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.

like image 537
John Henckel Avatar asked May 03 '17 21:05

John Henckel


People also ask

What is the default git merge strategy?

Recursive is the default merge strategy when pulling or merging one branch. Additionally this can detect and handle merges involving renames, but currently cannot make use of detected copies. This is the default merge strategy when pulling or merging one branch.

Does git stash pop do a merge?

But if a git stash pop conflict arises, then the problematic files won't be added to the index. Instead, Git populates the conflicted files with content from both the local working tree and the stash. The stash entry is kept in case you need it again. There's no magic remedy for such merge conflicts.


2 Answers

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.

like image 74
tmandry Avatar answered Oct 01 '22 18:10

tmandry


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.)

like image 45
torek Avatar answered Oct 01 '22 19:10

torek