972

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?

3
  • 8
    Git is smart enough not to drop a stash if it doesn't apply cleanly
    – Snowmanzzz
    Commented Mar 15, 2022 at 8:18
  • 1
    ... and knowing that your stash is still on the git stash list, be free to use git reset --hard. i.e. when you stash-poped on the wrong branch and your conflict is obvious. Commented Mar 7, 2023 at 17:46
  • 1
    Here's a duplicate question with a far simpler resolution to stash pop conflicts with a limited number of files affected stackoverflow.com/a/60348183/5440638
    – Kay V
    Commented Oct 23, 2023 at 18:21

6 Answers 6

1103

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:

  1. To unstage the merge conflicts: git reset HEAD . (note the trailing dot)
  2. To save the conflicted merge (just in case): git stash
  3. To return to master: git checkout master
  4. To pull latest changes: git fetch upstream; git merge upstream/master
  5. To correct my new branch: git checkout new-branch; git rebase master
  6. To apply the correct stashed changes (now 2nd on the stack): git stash apply stash@{1}
11
  • 20
    Excellent thanks! #6 is really the answer I was looking for. Could add a git stash drop as the last step to get rid of the unwanted stash from #2. Commented Oct 17, 2014 at 0:39
  • 3
    #2 will not work if there are unmerged paths, it will instead output the error described here: stackoverflow.com/questions/5483213/…
    – Étienne
    Commented Apr 30, 2015 at 14:25
  • 8
    Not totally true--Git will keep the stash in the stash list if it failed to apply cleanly. See these docs about git stash pop: "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." (git-scm.com/docs/git-stash) Commented Sep 13, 2016 at 19:46
  • 2
    This doesn't work properly. :-( It undid the changes but it left a bunch of noise in my files like: <<<<<<< Updated upstream >>>>>>> Stashed changes
    – Jez
    Commented Nov 13, 2017 at 0:13
  • 34
    @call-me This is something I think people should actually do more often! Notice how the "Ask a question" form has a checkbox that let's you answer your own question in the same form :). This happens to me sometimes when I'm in the process of authoring a question that is stumping me, but in trying to tune the question to be good for StackOverflow, I end up figuring out the solution. This was a while ago, so I don't remember if that was the case here, but I'm guessing that's what happened.
    – acjay
    Commented May 18, 2018 at 14:35
530

Luckily git stash pop does not change the stash in the case of a conflict!

So nothing, to worry about, just clean up your code and try it again.

Say your codebase was clean before, you could go back to that state with: git checkout -f
Then do the stuff you forgot, e.g. git merge missing-branch
After that just fire git stash pop again and you get the same stash, that conflicted before.

Keep in mind: The stash is safe, however, uncommitted changes in the working directory are of course not. They can get messed up.

10
  • 38
    What I understand is that you can simply cleanup and pop again, but you can't undo it. If the pop gets mixed with other uncommitted changes, then you have to clean up manually.
    – haridsv
    Commented Mar 24, 2017 at 6:22
  • 1
    @TrevorHickey The point of this answer was that the stash stays as it is in case of a conflict, that you can call git stash pop as often you need until it ends without a conflict. So after your conflict, yes, the working directory is in mess, however, you can clean it up and call git stash pop again.
    – flori
    Commented Aug 14, 2018 at 7:35
  • 19
    this command to undo last stash apply is very useful: git checkout -f !
    – Lafi
    Commented Feb 19, 2019 at 15:41
  • 3
    what does git checkout -f mean?
    – Aparna
    Commented Jun 11, 2019 at 8:52
  • 1
    git checkout -f branchname means "checkout branchname, and f = force, meaning I know there are uncommitted changes where I am right now, I don't care just dump everything. Since git stash pop doesn't actually "pop" if there are conflicts, I was able to do the force checkout and then git stash pop again on the correct branch. Easy peasy.
    – Mike K
    Commented Jul 22, 2021 at 22:09
140

The simplest command, it works everywhere including git stash pop, git merge etc.

But careful! You'll lose all changes on untracked files. Tracked files stays intact

git reset --merge
5
  • 34
    Careful here! You'll lose all changes on untracked files. Commented May 9, 2021 at 6:41
  • 5
    And you'll lose all changes on staged, tracked files too it seems.
    – enocom
    Commented Jul 23, 2021 at 21:40
  • 1
    This was exactly what I needed, since I've had changes in tracked files, so I didn't want a hard reset, and they were left in place.
    – tishma
    Commented Oct 21, 2022 at 8:18
  • This is dangerous! I ran this after running git stash pop while I already had files indexed, and I lost all of my indexed files! Commented Mar 31, 2023 at 10:38
  • TOO BAD I DIDNT RESEARCH WHAT UNTRACKED FILES WERE BEFOREHAND!!!! Good Friggin grief!!!!!!!!!!! Commented Mar 1 at 17:26
47

Instructions here are a little complicated so I'm going to offer something more straightforward:

  1. git reset HEAD --hard Abandon all changes to the current branch

  2. ... Perform intermediary work as necessary

  3. git stash pop Re-pop the stash again at a later date when you're ready

0
38
git checkout -f

must work, if your previous state is clean.

CAUTION: Beware–you'll lose all untracked changes to your files.

2
  • 9
    Beware–you'll lose all untracked changes to your files.
    – sbrk
    Commented Nov 18, 2021 at 17:09
  • well this removed some of my tracked, staged, but not committed changes!
    – Ali80
    Commented Dec 10, 2022 at 12:39
2

If you're like me and had unstaged changes that you want to preserve, you can avoid losing that work by checking out a known stable version of each individual file from the stash. Hopefully those files are different than the ones you were working on. Also, this is why we use small commits as we go, dumb dumb.

git checkout main -- <file_with_conflicts>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.