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:
| Tool | What It Does | Success Rate |
|---|---|---|
git reflog | Shows recent stash operations with commit hashes | High (if recent) |
git fsck --unreachable | Lists all dangling/unreachable objects | High (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
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
applybeforedrop, labeling stashes) is always better.
Code
# ─── 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 safelyKey Takeaways
- Dropped stashes aren't immediately deleted — the commit exists for ~30 days.
- Use
git reflogto find recent stash operations and their commit hashes. - Use
git fsck --unreachableas a deeper search for lost stash commits. - Prevention is better: Use
apply + verify + dropinstead ofpopfor important stashes.
Interview Prep
-
Q: How can you recover a stash that was accidentally dropped? A: Use
git reflogto find the stash's commit hash, thengit stash apply <sha>to restore it. Alternatively,git fsck --unreachable | grep commitfinds 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 thegc.reflogExpireandgc.pruneExpireconfiguration settings. -
Q: What is the safest way to handle stashes to avoid accidental loss? A: Use
git stash apply(which keeps the stash) instead ofgit stash pop. Verify the changes were applied correctly, then explicitlygit stash dropthe entry. This way, you never lose work if something goes wrong during application.