Programming

Recover Dropped Git Stash - Find and Restore Changes

Recover a dropped git stash: find its hash via git stash pop output, git fsck or reflog; inspect with git show/gitk, then apply or recreate the stash ref.

1 answer 4 views

How can I recover a dropped stash in Git?

I frequently use git stash and git stash pop to save and restore changes in my working tree. Yesterday, I had some changes in my working tree that I had stashed and popped, and then I made more changes to my working tree. I’d like to go back and review yesterday’s stashed changes, but git stash pop appears to remove all references to the associated commit.

I know that if I use git stash then .git/refs/stash contains the reference of the commit used to create the stash. And .git/logs/refs/stash contains the whole stash. But those references are gone after git stash pop. I know that the commit is still in my repository somewhere, but I don’t know what it was.

Is there an easy way to recover yesterday’s stash commit reference?

Yes — you can usually recover a dropped git stash by finding the stash’s dangling commit object and then applying it or recreating the stash ref. If your terminal showed a “Dropped refs/stash@{0} (hash)” from a prior git stash pop, copy that hash and run git stash apply <hash> or git checkout -b recovered-stash <hash>. If you closed the terminal, use git fsck / reflogs to list unreachable commits, inspect them with git show or gitk, then either git stash apply <hash>, create a branch from the commit, or restore the stash ref with git update-ref. Act quickly — if garbage collection has pruned those objects, they may be gone.


Contents


Recover a dropped git stash — quick overview

Lost a stash after git stash pop? You’re not alone — pop removes the stash ref but usually leaves the underlying commit(s) as dangling (unreferenced) objects. The recovery path is simple in concept:

  • Locate the stash commit hash (either from the pop output or by searching for dangling commits).
  • Inspect the commit to confirm it’s the stash you want.
  • Restore it by applying the commit (git stash apply <hash>), by creating a branch from it, or by recreating the stash reference with git update-ref.

The common tools used are git fsck, git reflog, git show (or GUI viewers like gitk), and git update-ref — see the step-by-step examples below and the linked references for variants and edge cases (e.g., stashes that include untracked files) from Stack Overflow and Baeldung.


If your terminal still shows the dropped hash (fastest fix)

If your git stash pop printed a line like:

Dropped refs/stash@{0} (59861637f7b599d87cb7a1ff003f1b0212e8908e)

you’ve already got the commit hash. Two quick options:

  1. Apply directly:
bash
git stash apply 59861637f7b599d87cb7a1ff003f1b0212e8908e
  1. Or create a branch to inspect/merge:
bash
git checkout -b recovered-stash 59861637f7b599d87cb7a1ff003f1b0212e8908e
git log -p -1

Either approach gets the changes back into your working tree. If you want the stash back in the stash list, use the update-ref method described below.

(See the concrete example in Baeldung’s recovery article.)


Find the lost git stash commit with git fsck or reflog

If you closed the terminal, run commands that list dangling/unreachable commit objects. Common patterns from community guides are:

List dangling commits (compact):

bash
git fsck --no-reflog | awk '/dangling commit/ {print $3}'

Or:

bash
git fsck --unreachable | grep commit | cut -d ' ' -f3

Those commands output candidate hashes. Stash commits often have messages that start with “WIP on :” (or your stash message), so inspect candidates with:

bash
git show <hash>
git show --name-only <hash>

To browse commits visually:

bash
git log --graph --oneline --decorate $(git fsck --no-reflog | awk '/dangling commit/ {print $3}')
gitk --all $(git fsck --no-reflog | awk '/dangling commit/ {print $3}')

If a stash reference had a reflog entry, git reflog stash might reveal it; but pop removes the ref and its reflog, so git fsck / unreachable-search is the reliable method. Community step-by-step examples use these exact tactics — see the Stack Overflow answer and the gist examples for variants and shell one-liners: Stack Overflow example and gist instructions.


Inspect and restore the stash: apply, branch or recreate refs

Once you identify the correct commit hash:

  1. Quick apply (no stash ref created):
bash
git stash apply <hash>

That applies the stash-like commit to your working tree.

  1. Or create a branch from that commit (safer for review):
bash
git checkout -b recovered-stash <hash>
# Inspect diffs:
git status
git diff HEAD~..HEAD # or git log -p -1
  1. Put it back into the stash list (recreate refs/stash):
    If you prefer the original stash entry to reappear in git stash list, recreate the refs/stash ref:
bash
git update-ref refs/stash <hash> -m "Recovered stash"
# If you don't see it, try:
git update-ref --create-reflog refs/stash <hash> -m "Recovered stash"
git stash list

The update-ref method is shown in community guides for re-attaching a dangling commit as a stash entry — see the two-step recovery that uses git update-ref in the DEV.to writeup.

Notes about multi-commit stashes:

  • A stash can involve multiple objects (index, worktree, and untracked commits). If you find multiple related commits, pick the one with the “WIP on…” message (usually the stash root). If needed, create a branch from that root and merge the others or cherry-pick changes into the branch.

If you prefer merging the recovered branch back into your current branch:

bash
git checkout main
git merge recovered-stash
# or manually copy files and commit

(For practical examples of creating a branch from a stash commit and applying its changes, see Graphite’s guide and PhoenixNAP’s how‑to.)


Visual search and file-content scanning examples

If you don’t recognize a commit by message, search its contents:

Dump all unreachable objects to a file and grep for filenames or strings you remember:

bash
git fsck --unreachable | awk '{print $3}' | xargs git show >> find.txt
grep -n "some-file-name-or-code-snippet" find.txt

Or build a log over all dangling commits and skim:

bash
git log --graph --decorate --pretty=oneline --abbrev-commit --all $(git fsck --no-reflog | grep commit | cut -d' ' -f3)

GUI tools like gitk make scanning faster:

bash
gitk --all $(git fsck --no-reflog | awk '/dangling commit/ {print $3}')

These approaches are used in multiple community answers and tutorials — see the Gist and Opensource examples for exact one-liners you can copy and paste: gist instructions, Opensource.com example.


When recovery fails — garbage collection and prevention tips

What if git fsck finds nothing? If git gc or git prune ran after the stash was dropped, it’s possible the dangling objects were removed and recovery is not possible. So — stop running destructive maintenance until you’ve searched.

Prevention and safer habits:

  • Use git stash push -m "meaningful message" (or git stash save -m on older Git) so stash entries are easy to identify. Community guides stress naming stashes for this reason.
  • Prefer git stash apply when you want to test a stash but keep it in the list; only pop removes the stash ref.
  • Use git stash branch <name> stash@{n} to create a branch and apply the stash in one step.
  • If you rely heavily on work-in-progress snapshots, consider committing to a temporary branch and pushing it to a remote, or using a local backup of .git before risky operations.

For authoritative background on stash usage (and to learn commands that keep you safer next time), read the official stash documentation and tutorials such as the Atlassian Git tutorial and the Git book.


Sources

  1. How do I recover a dropped stash in Git? - Stack Overflow
  2. How to recover a dropped stash in Git? - gist (joseluisq)
  3. How to Recover a Dropped Stash in Git - Baeldung on Ops
  4. How to recover a Git stash - Graphite guide
  5. Recover a lost Git stash in two steps - DEV Community
  6. How to Recover a Dropped Stash in Git? - GeeksforGeeks
  7. How to Drop a Git Stash and Recover a Deleted Stash - PhoenixNAP
  8. How to recover from a git mistake - Opensource.com
  9. Git - Stashing and Cleaning (Git book, ru translation)
  10. git stash - Saving Changes - Atlassian Git Tutorial

Conclusion

You can usually recover a dropped git stash by locating the stash’s dangling commit with git fsck (or from your git stash pop output), inspecting it with git show/gitk, and then applying it or restoring the stash ref with git update-ref or by creating a branch from the commit. If garbage collection has already pruned the unreachable objects, recovery may not be possible — so act quickly and prefer git stash apply (or named stashes) when you want to keep a safety copy.

Authors
Verified by moderation
Moderation
Recover Dropped Git Stash - Find and Restore Changes