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

How to recover 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.


Accidentally deleting or dropping a stash in Git can be stressful, especially if the stash contains important changes. Luckily however, Git provides mechanisms to recover these stashes under certain conditions. This guide will explore how to recover a Git stash,including how to recover deleted and dropped stashes.

Git stash operates on a LIFO (last in, first out) basis and is a temporary storage for changes that are not yet ready to be committed. If a stash is dropped or deleted, it's removed from the list of stashes but can often still be found in the repository's reflog, unless it has been pruned or garbage collected by Git.

Git maintains a log of where your HEAD and branch references have been recently, which includes stashed changes. You can access this log with:

Terminal
git reflog

Look through the output for entries related to stashing, such as "stash@{1}: WIP on...". Each entry is associated with a commit ID.

Once you find the entry corresponding to the dropped or deleted stash, note the commit ID associated with it. This ID is essential for the recovery process.

To safely inspect and recover the stash, create a new branch pointing to the selected commit ID:

Terminal
git branch recover-branch <commit-ID>

Switch to this new branch:

Terminal
git checkout recover-branch

Before fully integrating the recovered stash into your project, inspect the changes:

Terminal
git log -p

This command shows the diff of what was in the stash. Using the diff, examine these changes and confirm that these are the changes you want to recover.

If the changes are what you were looking for, you can apply them to your branch. Since you're on a dedicated recovery branch, you can merge these changes back into your main working branch or re-stash them:

Terminal
git stash apply

Or, if you are ready to integrate these changes:

Terminal
git checkout main
git merge recover-branch

Once recovery is successful and you've merged the changes back to your main branch, you can delete the temporary recovery branch:

Terminal
git branch -d recover-branch
  • Frequent backups: Regularly push your stashes to a remote repository or create patches as backups.
  • Reflog housekeeping: Remember that the reflog is maintained for a default of 90 days in most configurations. Recover lost stashes within this period.
  • Avoid garbage collection: Be cautious with commands like git prune that force garbage collection, as these may permanently delete stashes that could otherwise be recoverable.

Recovering a git stash involves using the reflog to find the lost stash's commit ID, creating a new branch from this commit, and then applying or merging the changes as needed. This process can help you retrieve valuable stashed changes that were accidentally dropped or deleted. Always ensure to keep regular backups of important stashes to mitigate the risks of loss.

For further reading, see this guide on stashing in Git.

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