Retrieve deleted stash in Git using SourceTree
Based on the above answers, here is a simple sequence:
Open a terminal window and cd into a folder under the repository. Then:
git fsck | awk '{print $3}' > tmp.txt
cat tmp.txt | xargs git show > tmp2.txt
Now open tmp2.txt in editor, locate your lost code, and find the commit-id on top of it. Then apply the code:
git stash apply <commit id>
rm tmp.txt tmp2.txt
This saved my life! I really thank all those who answered this question. I bless the git creator Linus Torvalds for keeping deleted stuff in the git database. Genius!!
EDIT 2021: note that Windows users can do the same using Git Bash.
The stash is saved internally as a merge commit referenced from a list of stashes.
git fsck
can find dangling objects. It will find not just your deleted stash, but probably other stuff, too... so you'll be wanting to look for commits that look like they could be your stash (git show <ID>
to display relevant info about an object and decide whether it's the one you're looking for).
Once you have that, all you need to do is re-insert it into the list of stashes. The list is stored in .git/logs/refs/stash
and a line has the following format:
<ID of previous stash commit in list or 0000000000000000000000000000000000000000 if none> <ID of merge commit> Your Name <[email protected]> <UNIX timestamp> <time zone, e.g. +0000><TAB char><description of stash>
Here's a working example:
16b9a2d400dafe7ea25592029e3e5582d025c7d8 5def7605dfe625e8b3a3152fe52a87cc36694b6a Jan Krüger <email.censored@invalid> 1374227992 +0200 WIP on master: 0dbd812 Update draft release notes to 1.8.4
Just synthesize a line for the stash you want to re-insert (the name/mail/timestamp/description don't have to be accurate) and you should be able to use it normally again.
Happy hunting!
Like the previous answer states, you can use git fsck
to list objects that
aren't referenced by anything which would include your deleted stash. But, it
is possible to use git show
to filter that list of objects to show only
stashes like:
git fsck 2> /dev/null |
awk '/commit/{print $3}' |
git show --stdin --merges --grep '^WIP on'
If you know when the stash was created, you could also add an argument like
--since '2 days ago'
to the final line to limit the output further. Hopefully that will cut the list down to a manageable size.
Once you've found the correct stash make note of its commit ID, and you can use
git stash apply COMMITID
to apply it as if it hadn't been deleted.
This is the most cleaner workaround to recover deleted stash.
git fsck --lost-found
ls -1 .git/lost-found/commit/ | xargs -n 1 git log -n 1 --pretty=oneline
git stash apply [tag]
Replace [tag] with the id, ex:
git stash apply 40e47250d0b4fb6143be67c115b708be126e79d3