Retrieve deleted stash in Git using SourceTree
GitAtlassian SourcetreeGit Problem Overview
I am using source tree. I had created a stash of multiple changes and by mistake deleted it. Is there a way to retrieve them back?
Git Solutions
Solution 1 - Git
Based on previous 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.
Solution 2 - Git
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 <your@email.example> <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!
Solution 3 - Git
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.
Solution 4 - Git
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
Solution 5 - Git
It might be helpful to use comments while stashing, using:
git stash save "comment"
It saved me some trouble finding already deleted stash using the following:
git fsck --lost-found
ls -1 .git/lost-found/commit/ | xargs -n 1 git log -n 1 --pretty=oneline
git stash apply [tag]
Solution 6 - Git
As Jan Krüger states above, git fsck
is the way to go. However, if you find yourself unable (for whatever reason) to successfully synthesize a line in your stash file and make the stash appear in the available list, you can use git stash apply <guid>
directly, without adding the line. This will immediately apply (not commit) the file(s) changes to your current branch.
Solution 7 - Git
Another solution is:
git fsck 2>&1 | awk '/dangling commit/{print $3 "^!"}' | xargs git log
find the author and commit informations(date,hash,author etc.)
git stash store <hash-id-of-specific-commit>
Solution 8 - Git
for i in $(git fsck 2>|/dev/null | grep commit | cut -d' ' -f3); do git --no-pager log -1 $i; echo "-------------------------"; done | less
Then find the commit id#.
and do
git stash apply {commit#}