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?
10 Answers
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.
-
Didn't know git's creator was Linus - I would like to bless him as well. He just saved my life too.– KaanSep 5 at 2:18
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!
-
I ran the
git fsck
command and it gave me around 100 guids. Is there a simpler way to get around this? I mean something more visual? Jul 19, 2013 at 10:10 -
1its impossible to go through 100 guids! Please suggest an easier way. Jul 19, 2013 at 10:25
-
4That's the only way I know. However, you can probably reduce it quite a bit using this:
git fsck --unreachable
; and to directly get a list with details, maybe something like this:for i in $(git fsck --unreachable --no-dangling 2>|/dev/null | grep commit | cut -d' ' -f3); do git --no-pager log -1 $i; done | less
Jul 19, 2013 at 10:46 -
2
-
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
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]
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>
-
I tried a few scripts but this one worked without errors or giving me random/n data. Jul 20, 2017 at 2:07
-
This worked to quickly find the dangling commit. In VSCode, clicking the commit id output quickly shows UI with many git commands.– whittetNov 23, 2022 at 19:13
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.
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#}
Just the Windows equivalent of Tengku Fathullah's answer
git fsck --lost-found
for /f %f in ('dir /b .git\lost-found\commit') do @git log -n 1 %f --pretty=oneline
git stash apply [tag]
(Where [tag] is the selected id from the second command).
If that's still a long list to hunt through, it can be piped into find
. For example for an unnamed stash:
for /f %f in ('dir /b .git\lost-found\commit') do @git log -n 1 %f --pretty=oneline | find "WIP"
Batch file notes, just for reference:
- As normal with the
for
command, note that would need to be%%f
(twice) in a batch file - The
@git log
is there to suppress command prompt echo, to give single line output