Recovering Deleted Git Stashes

Recovering Deleted Git Stashes

A dropped Git stash isn't gone until garbage collection runs — how to find it as a dangling commit and cherry-pick it back.

Takahiro Iwasa
2 min read

A Git stash is implemented internally as a commit. Consequently, dropping a stash does not delete the underlying object immediately; it persists in the repository as a “dangling” commit until garbage collection runs. This makes recovery possible in most cases.

Output Commit History

Use the following command to identify dangling commits in the repository:

Terminal window
git fsck | awk '/dangling commit/ {print $3}' >> commit_list.txt

The output will look something like this:

dangling commit bfebf68feeebf07a86d7e3e4da77962de67c14ee
dangling commit 86f4aec78bd51c80b0fd2d5d83963a2259dc72b4
dangling commit 48fd9f8f97a577bc8a87b133f6e1dd789a692bd0
dangling commit 64ff8c33fb878afb8bf5c99c1b8d8fdfaa1b1f3c
dangling commit bdff88e13803e6c7737691aa1fb6f1e038321966

Output Commit Summaries

To review the details of each candidate commit, run the following script:

#!/bin/bash
while read line
do
git show $line
done < ./commit_list.txt

This will display summaries of the dangling commits:

commit bfebf68feeebf07a86d7e3e4da77962de67c14ee
Merge: b046240 5ee731a
Author: Takahiro Iwasa <[email protected]>
Date: Fri Feb 12 22:01:47 2016 +0900
On develop: 0212

Restore Selected Commits

Review the commit summaries and use the date, time, and commit message to identify the commits to be restored.

The identified commits can then be restored using git cherry-pick:

Terminal window
git cherry-pick -n -m1 <YOUR_COMMIT_ID>

Conclusion

Listing dangling commits, reviewing their summaries, and cherry-picking the right one back in recovers a stash that git stash drop appeared to have deleted. git fsck | awk '/dangling commit/ {print $3}' is the key step: it turns what feels like an unrecoverable mistake into a short list of commit IDs to sift through, since Git doesn’t actually delete stash objects on drop, just the ref pointing to them. Walking that list with git show to read the author, date, and message is usually enough to identify the right commit even without knowing its hash in advance, and git cherry-pick -n -m1 reapplies the stash’s changes to the working directory without creating a new commit. The catch is that this only works until garbage collection runs, so recovery is a race against git gc rather than a guarantee.

About the author

Takahiro Iwasa

Takahiro Iwasa

Software Developer

This blog shares technical notes from hands-on projects—architecture, implementation, and AWS service integrations.