Data report"State of code review 2024" is now liveRead the full report

How to undo git stash

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

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


Sometimes after applying or popping a Git stash, you may realize that you need to revert those changes. This guide will provide detailed instructions on how to undo git stash actions such as undo git stash pop, git undo stash, and undo git stash apply, ensuring you can safely manage and reverse changes as needed.

Git stash temporarily saves modified, tracked files and staged changes, allowing you to switch branches without committing. The stash is locally stored and can be listed, applied, or even removed.

Undoing a git stash operation can mean two things: reversing the effects of applying a stash to your working directory, or restoring a dropped stash. Here we will focus on the first scenario.

When you use git stash apply, the stash is not removed from the stash list, which gives you flexibility in accessing it later. If you decide to undo the application of a stash, you can reset or revert the changes without losing them.

Before undoing the stash application, check your current changes:

Terminal
git status

If the changes are not yet staged (not added to the index), you can reset them:

Terminal
git reset --hard

This command resets the index and working tree. Any changes to tracked files in the working tree since the last commit are discarded. Because you used git stash apply instead of git stash pop the stash will still be present in the stash list, keeping your changes intact.

Git stash pop applies the stash and then removes it from the stash list. To undo a pop, you need to deal with the fact that the stash is no longer in the list.

If you immediately realize the mistake after popping the stash, you can typically find the commit ID of the stash in the terminal output or via:

Terminal
git reflog

Look for an entry like "stash apply: WIP on branch...". Note the commit ID.

To inspect and potentially revert to the stash, create a temporary branch from it:

Terminal
git branch temp-branch <commit-ID>

Now, check out this branch and revert the changes as needed:

Terminal
git checkout temp-branch
git reset --hard HEAD^
  • Using git revert for committed changes: If the changes from the stash were committed, you can use git revert to create a new commit that undoes the previous commits.

  • Manual undo for specific files: For specific file changes, check out previous versions of the files:

    Terminal
    git checkout HEAD -- path/to/file
  • Test before popping a stash: Always check what's in a stash before popping it, especially if you're not sure about its contents:

    Terminal
    git stash show -p stash@{0}
  • Backup stashes: Consider copying important stashes to another branch or tagging them before applying or popping, to prevent data loss.

  • Regular commits: Regularly commit your work to avoid reliance on stashes for long-term changes.

Undoing git stash operations such as git undo stash pop and git undo stash apply requires careful handling to ensure that changes are reverted correctly and that valuable work is not lost. By understanding the mechanics of git stash and how to effectively manage your working directory and index, you can confidently revert any applied or popped stashes.

Git gud
"It's the first Git workflow I've used that actually feels good."
–@robboclancy
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