Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve deleted stash in Git using SourceTree

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?

like image 282
tusharmath Avatar asked Jul 19 '13 09:07

tusharmath


1 Answers

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.

like image 66
ishahak Avatar answered Sep 16 '22 17:09

ishahak