I popped a stash and there was a merge conflict. Unlike the question that is listed as a duplicate, I already had some uncommitted changes in the directory which I wanted to
My use case: just tried popping onto the wrong branch and got conflicts. All I need is to undo the pop but keep it in the stash list so I can pop it out on the correct branch. I did this:
git reset HEAD --hard
git checkout my_correct_branch
git stash pop
Easy.
If there were no staged changes before the git stash pop
, as in the question, then the following two commands should work.
git diff --name-only --cached | xargs git checkout --ours HEAD
git ls-tree stash@{0}^3 --name-only | xargs rm
The first reverses any merges from the stash, successful or not. The second deletes any untracked files introduced by the stash.
From man git stash
: The working directory must match the index.
Which @DavidG points out, the stash pop
will fail if any currently unstaged modified files conflict. As such, we shouldn't need to worry about unwinding merge conflicts beyond getting back to HEAD
. Any remaining modified files are then unrelated to the stash, and were modified before the stash pop
If there were staged changes, I'm unclear on whether we can rely on the same commands and you may want to try @Ben Jackson's technique. Suggestions appreciated..
Here is a testing setup for all of the various cases https://gist.github.com/here/4f3af6dafdb4ca15e804
# Result:
# Merge succeeded in m (theirs)
# Conflict in b
# Unstaged in a
# Untracked in c and d
# Goal:
# Reverse changes to successful merge m
# Keep our version in merge conflict b
# Keep our unstaged a
# Keep our untracked d
# Delete stashed untracked c
Use git reflog
to list all changes made in your git history. Copy an action id and type git reset ACTION_ID
Some ideas:
Use git mergetool
to split the merge files into original and new parts. Hopefully one of those is the file with your non-stash changes in it.
Apply the diff of the stash in reverse, to undo just those changes. You'll probably have to manually split out the files with the merge conflicts (which hopefully the above trick will work for).
I didn't test either of these, so I don't know for sure of they will work.
If DavidG is correct that it didn't pop the stash because of the merge conflict, then you merely need to clean up your working directory. Quickly git commit
everything you care about. (You can reset
or squash
the commit later if you're not done.) Then with everything you care about safe, git reset
everything else that git stash pop
dumped into your working directory.
Edit: From the git help stash
documentation in the pop section:
Applying the state can fail with conflicts; in this case, it is not removed from the stash list. You need to resolve the conflicts by hand and call git stash drop manually afterwards.
If the --index option is used, then tries to reinstate not only the working tree's changes, but also the index's ones. However, this can fail, when you have conflicts (which are stored in the index, where you therefore can no longer apply the changes as they were originally).
Try hardcopying all your repo into a new dir (so you have a copy of it) and run:
git stash show
and save that output somewhere if you care about it.
then: git stash drop
to drop the conflicting stash
then: git reset HEAD
That should leave your repo in the state it was before (hopefully, I still haven't been able to repro your problem)
===
I am trying to repro your problem but all I get when usin git stash pop
is:
error: Your local changes to the following files would be overwritten by merge:
...
Please, commit your changes or stash them before you can merge.
Aborting
In a clean dir:
git init
echo hello world > a
git add a & git commit -m "a"
echo hallo welt >> a
echo hello world > b
git add b & git commit -m "b"
echo hallo welt >> b
git stash
echo hola mundo >> a
git stash pop
I don't see git trying to merge my changes, it just fails. Do you have any repro steps we can follow to help you out?