Undo git stash pop that results in merge conflict

git, git-stash

Solution

As it turns out, Git is smart enough not to drop a stash if it doesn't apply cleanly. I was able to get to the desired state with the following steps:

- To unstage the merge conflicts: `git reset HEAD .` (note the trailing dot)

- To save the conflicted merge (just in case): `git stash`

- To return to master: `git checkout master`

- To pull latest changes: `git fetch upstream; git merge upstream/master`

- To correct my new branch: `git checkout new-branch; git rebase master`

- To apply the correct stashed changes (now 2nd on the stack): `git stash apply stash@{1}`

Problem

I began making changes to my codebase, not realizing I was on an old topic branch. To transfer them, I wanted to stash them and then apply them to a new branch off of master. I used `git stash pop` to transfer work-in-progress changes to this new branch, forgetting that I hadn't pulled new changes into master before creating the new branch. This resulted in a bunch of merge conflicts and loss of a clean stash of my changes (since I used pop). Once I recreate the new branch correctly, how I can I recover my stashed changes to apply them properly?

Original source

Related problems