Lesson Completion
Back to course

Recovering from Stash Mistakes

Beginner
8 minutes4.7Git

The Hook (The "Byte-Sized" Intro)

You accidentally ran git stash drop on a stash with 3 hours of work. Stomach drops. But wait — Git doesn't immediately delete anything. The stash commit still exists in Git's object database. It's just unreachable. For about 30 days, you can find it using git reflog or git fsck. Here's the rescue playbook.

📖 What is Recovering from Stash Mistakes?

When you accidentally drop or clear stashes, the underlying commit objects aren't immediately deleted. Git's garbage collector runs periodically (usually after 30 days) to clean up unreachable objects. Until then, you can find and recover them.

Conceptual Clarity

Recovery tools:

ToolWhat It DoesSuccess Rate
git reflogShows recent stash operations with commit hashesHigh (if recent)
git fsck --unreachableLists all dangling/unreachable objectsHigh (below 30 days)
git stash apply <sha>Re-applies a stash by its commit hash✅ Works
git branch recovery <sha>Creates a branch pointing to the lost stash✅ Works

Time limit: Git's garbage collector (git gc) removes unreachable objects after ~30 days. Act quickly.

Real-Life Analogy

Dropping a stash is like deleting a file on your computer. It's gone from the folder view, but the data is still on the disk until overwritten. git reflog and git fsck are like file recovery tools — they scan the "disk" and find the data before it's permanently gone.

Visual Architecture

flowchart LR DROP["🗑️ Stash Dropped"] --> REFLOG["git reflog"] DROP --> FSCK["git fsck --unreachable"] REFLOG --> SHA["Find commit SHA"] FSCK --> SHA SHA -->|"git stash apply SHA"| RESTORED["✅ Changes Restored"] style DROP fill:#2d1b1b,stroke:#e94560,color:#e94560 style SHA fill:#1a1a2e,stroke:#ffd700,color:#ffd700 style RESTORED fill:#1b2d1b,stroke:#53d8fb,color:#53d8fb

Why It Matters

  • Accident recovery: Everyone makes mistakes — knowing recovery gives confidence.
  • Time-sensitive: The 30-day window means you must act before garbage collection.
  • Professional skill: Recovering lost work is a high-value skill that impresses teammates.
  • Last resort: Prevention (using apply before drop, labeling stashes) is always better.

Code

bash
# ─── Method 1: Find via reflog ─── git reflog # Look for entries like: # a1b2c3d stash@{0}: WIP on main: abc1234 My commit message # The SHA (a1b2c3d) is your recovery key # ─── Recover by applying the SHA ─── git stash apply a1b2c3d # Your changes are restored! # ─── Method 2: Find via fsck (if reflog doesn't help) ─── git fsck --unreachable | grep commit # Output: # unreachable commit a1b2c3d4e5f6... # unreachable commit f6e5d4c3b2a1... # Inspect each to find your stash: git show a1b2c3d4e5f6 # Look for your changes in the diff output # Apply the right one: git stash apply a1b2c3d4e5f6 # ─── Method 3: Create a branch from the lost stash ─── git branch recovered-stash a1b2c3d git switch recovered-stash # Now your stashed changes are safely on a branch # ─── Prevention: use apply before drop ─── git stash apply stash@{0} # Apply first (keep copy) # Verify everything works... git stash drop stash@{0} # THEN drop safely

Key Takeaways

  • Dropped stashes aren't immediately deleted — the commit exists for ~30 days.
  • Use git reflog to find recent stash operations and their commit hashes.
  • Use git fsck --unreachable as a deeper search for lost stash commits.
  • Prevention is better: Use apply + verify + drop instead of pop for important stashes.

Interview Prep

  • Q: How can you recover a stash that was accidentally dropped? A: Use git reflog to find the stash's commit hash, then git stash apply <sha> to restore it. Alternatively, git fsck --unreachable | grep commit finds dangling commit objects that may include the lost stash.

  • Q: How long do you have to recover a dropped stash? A: Typically around 30 days, until Git's garbage collector (git gc) removes unreachable objects. The exact timing depends on the gc.reflogExpire and gc.pruneExpire configuration settings.

  • Q: What is the safest way to handle stashes to avoid accidental loss? A: Use git stash apply (which keeps the stash) instead of git stash pop. Verify the changes were applied correctly, then explicitly git stash drop the entry. This way, you never lose work if something goes wrong during application.

Topics Covered

Git StashingGit Recovery

Tags

#git#stash#reflog#recovery

Last Updated

2026-02-12