How to undo git stash pop

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


git stash is a tool in Git that temporarily shelves (or stashes) changes you've made to your working directory allowing you to work on something else, and then come back and re-apply them later on. However, sometimes after running git stash pop, you might realize that you either need to undo this action or manage unexpected conflicts. This guide explores how to effectively undo a git stash pop.

When you execute git stash pop, Git attempts to reapply the changes stashed most recently and then removes the stashed changes from the stash list. This operation is risky in that it automatically attempts to clear the stash once the pop is complete, which can complicate attempts to undo it if something goes wrong.

git stash pop will attempt to merge stashed changes back into your working directory. If there are no conflicts, it will successfully apply the changes. If conflicts arise, Git will stop the process for you to manually resolve them, but the stash will not automatically be removed if conflicts prevent the stash from being fully applied.

Undoing a git stash pop involves several steps, depending on what exactly you need to reverse or recover:

If git stash pop was successful and you want to revert the changes it made, you can use:

Terminal
git reset --hard

This command resets the working directory to the last commit state, erasing all changes made since. However, it’s crucial to understand that this method is destructive to any uncommitted changes in your working directory.

If you need to get the stash back because you popped it accidentally then discarded those changes you can:

  1. Check if Git still has a reference to the stash commit:

    Terminal
    git fsck --no-reflog | grep stash
  2. If you find a stash commit (something like dangling commit xyz123), you can inspect it:

    Terminal
    git stash show -p xyz123
  3. If this is the stash you lost, apply it again:

    Terminal
    git stash apply xyz123

This will recover the stashed changes and apply them to your working directory.

If you popped the stash and encountered conflicts, Git will not remove the stash from the stash list. In this case:

  1. Resolve the conflicts as you would after a merge conflict.
  2. Decide whether to keep the stash:
    • If you want to keep the stash, simply leave it in the stash list.
    • If you no longer need the stash, run git stash drop to remove it once you're satisfied with the conflict resolution.

The git reflog can also be a helpful tool if you've lost track of what has happened after various operations:

Terminal
git reflog

Look for entries related to stash application, and reset your branch to a point before the stash was popped:

Terminal
git reset --hard HEAD@{n}

Where n is the index in the reflog before the stash was applied.

For further reading on this subject see "Every engineer should understand git reflog".

Undoing a git stash pop can be complex, especially if conflicts are involved or if the stash was removed successfully. Regularly committing changes or using git stash apply instead of git stash pop (which doesn't remove the stash) can provide more flexibility and reduce the need for complex recovery operations. By understanding these techniques, you can better manage your Git stashes and recover from mistakes more effectively.

For further reading on undoing a git stash pop see the official Git documentation.

Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2