How do I pop git stash without triggering an auto-merge?

I belatedly realized that git already provides an extremely simple solution to the problem that motivated this question (namely an automatic merge with the potential to put the repository in a "unmerged state").

All one needs to do is to use

git stash branch <branchname> [<stash>]

instead of git stash pop (or git stash apply). Where <branchname> is the name of a new branch created by git for the purpose of applying the stashed changes.

This pops the stash in a way that is guaranteed to be free of conflicts.


OK, after running into something like this:

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

# $#@?!?!%$!*@#...

...the best solution that I've managed to come up with is to respond with this:

% git checkout --theirs $(git diff --name-only --diff-filter=U)
% git reset
% git stash drop

(Based on this answer.)


stash merges, that's just how it works.

You can achieve the non-merge-y stash with with write-tree read-tree and checkout-index. Here's a worked example for doing this to achieve a pristine test environment.

To just brute-force a no-merge stash apply, you could e.g.

git read-tree stash^{tree}
git checkout-index -af

The fact that you are seeing a merge conflict means that there was a change in the foo file that you pulled from the server. So, copying your file over and moving back will completely nuke all changes from the repo in the foo file. If you commit that, the other person that committed changes to foo will hate you.

The answer to your question depends on what you are trying to accomplish. Are you trying to see what the changes on the server were compared to your code? Are you trying to avoid dealing with other people's code until you are done?

If you just want to see what the change to the branch were, you can use git fetch instead of git pull and compare your current code with that. Or, if you don't want to merge your changes now, consider working in a separate branch, or not pulling until you are ready to merge.

Tags:

Git