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

How to restore a 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.


When working with Git, stashing changes is a helpful way to temporarily save them without committing. This guide will explore how to restore a git stash, ensuring that you can confidently manage your changes and integrate them back into your active branch.

Git stash temporarily shelves (or stashes) changes you've made to your working directory so you can work on something else, then come back and re-apply them later on. The stashed changes are stored in a stack-like structure managed by Git, allowing multiple stashes at the same time.

Before restoring stashes, it's important to know how to create them. Here’s how to stash your current changes:

Terminal
git stash push -m "Message describing the stash"

You can include untracked files with your stash by adding the -u or --include-untracked option:

Terminal
git stash push -u -m "Include untracked files"

To see a list of all your stashes, use:

Terminal
git stash list

This command displays all stashes stored in the stash stack. Each entry is indexed starting from 0.

To apply a stashed change and remove it from the stash stack, use:

Terminal
git stash pop

If you want to specify a particular stash, you can provide the stash ID, which you can find using git stash list:

Terminal
git stash pop stash@{0}

If you want to apply changes from a stash but keep the stash in your stack for future use, use:

Terminal
git stash apply

To apply a specific stash:

Terminal
git stash apply stash@{0}

To apply the changes exactly as they were stashed, including staged changes, use the --index flag:

Terminal
git stash apply --index

This means that any changes you had staged (added to the index) when you created the stash will remain staged after you apply the stash, preserving the state of your workspace precisely.

If you decide you no longer need a stash, you can remove it from the stack with:

Terminal
git stash drop stash@{0}

To remove all stashed entries:

Terminal
git stash clear
  • Verify before popping: Always review the stash content (git stash show -p stash@{0}) before applying it to avoid merging unwanted changes.
  • Regularly prune stashes: Manage your stash list by clearing out old or unnecessary stashes to keep your repository clean.
  • Backup before apply: Before applying a stash to a working directory with other changes, consider committing current changes to avoid conflicts.

For further reading on the git stash command see the official Git documentation.

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