96

I executed git stash save "ABC".
Then by mistake I did git stash clear . How can I retrieve the data that was in stash ABC?

1
  • 1
    None of the answers worked for me, but the one explained here did. Thought it could help others, too.
    – Val
    Apr 4, 2019 at 13:04

6 Answers 6

153

As it may be found in the documentation of git stash, you may be lucky if this works:

Recovering stashes that were cleared/dropped erroneously

If you mistakenly drop or clear stashes, they cannot be recovered through the normal safety mechanisms. However, you can try the following incantation to get a list of stashes that are still in your repository, but not reachable any more:

git fsck --unreachable | grep commit | cut -d\ -f3 | xargs git log --merges --no-walk --grep=WIP

If you find the stash you cleared by mistake, then you can do:

git stash apply <stash>

EDIT: Use this command instead git fsck --unreachable | grep commit | cut -d ' ' -f3 | xargs git log --merges --no-walk --grep=WIP

6
  • 6
    @AshishBanker: Then it's gone. Let this be a lesson to you. Don't use git stash to store changes you care about, use git commit, even if it's partial. You can always git commit --amend and git rebase -i afterwards. Sep 30, 2015 at 13:36
  • 23
    On OSX: git fsck --unreachable | grep commit | cut -d ' ' -f3 | xargs git log --merges --no-walk --grep=WIP Nov 4, 2016 at 9:41
  • 3
    The arguments of cut are not correct. There should be two spaces after the \. This is because the markdown interpreter removes one of the spaces. As suggested by @JJS use -d ' ' or -d " " as first argument of cut.
    – chmike
    Apr 13, 2017 at 16:42
  • 1
    I've done this mistake via IntelliJ IDEA. Luckily it has its own VCS. So you can use VCS > Local History > Show History per each file you changed (yes, you must remember each you've changed). Jul 17, 2017 at 15:08
  • 5
    while using git bash on Windows, It worked for me like a charm. It is quite powerful tool to recover stashes. git stash apply <commit SHA>. Did not feel the need of git stash pop because I accidentally cleared my stash list.
    – Jitendra
    Sep 28, 2017 at 11:30
55

All of the above answers end with a git stash apply [commit] which is good, but is not an exact undo of git stash clear. For that you need to re-stash the orphaned stash-commit. I found these instructions which almost worked but needed a flag to get all the way there. Summarizing:

  1. Find the orphaned stash commits: git fsck --unreachable | grep commit | cut -d ' ' -f3 | xargs git log --merges --no-walk
  2. Re-stash the commit: git update-ref --create-reflog refs/stash 4b3fc45c94caadcc87d783064624585c194f4be8 -m "My recover stash"
2
  • Great one @studog! worked for me. I would add 3. git stash apply Jan 4 at 15:42
  • @JoseRondon git stash apply would apply the stash to your working tree, which is beyond the scope of restoring an accidentally dropped stash. However, yes, a good method to protect against repeated mistakes is to apply the stash and then commit it instead of stashing.
    – studog
    Jan 11 at 20:24
8

Run this command to find the commit:

git fsck --unreachable | grep commit | cut -d ' ' -f3 | xargs git log --merges --no-walk --grep=WIP

will list something like:

Checking object directories: 100% (256/256), done.
commit c36e565014d9a927c36f16e78bc327eb375d33b8
Merge: dff6bc1 4e05a0c
Author: suhailvs <[email protected]>
Date:   Thu Jul 19 13:32:01 2018 +0530

WIP on master: dff6bc1 added menu

then checkout that commit c36e565014:

git checkout c36e565014
8

I had to use

git fsck --unreachable | grep commit | cut -d ' ' -f3 | xargs git log --merges --no-walk

to fetch all the orphan stash which will give an o/p similar to this:

Checking object directories: 100% (256/256), done.
Checking objects: 100% (395/395), done.
commit 3928306034b292770cc4cd2987c034ffad250e0b    //commit stash hash
Merge: 16056a0 ac3c354
Author: Jimmy <mail>
Date:   Thu Nov 14 17:31:05 2019 +0530

    On profile: stashing for

commit 50f6f3a7161dd44bfcef2b8328a2329db4c7ec34

and use

git apply stash 3928306034b292770cc4cd2987c034ffad250e0b

And I got my stashed changes back. Thanks to https://mobilejazz.com/blog/how-to-recover-a-deleted-git-stash/

2
  • 3
    I think that for your last command the correct way to right it is: 'git stash apply 3928306034b292770cc4cd2987c034ffad250e0b'
    – Djamel F.
    Feb 21, 2020 at 14:14
  • @DjamelF. that's the right way to correct it ;) Dec 1, 2020 at 13:24
0

I also deleted a stash, but using the GitKraken gui, so I don't know exactly what git commands it executed. The chosen answer didn't work for me but put me on the right path at least.

In my case, manually searching through --unreachable objects worked. I'm sure there is a more efficient way, but I'm just glad I was able to recover the changes.

ids=`git fsck --unreachable | grep blob | cut -d ' ' -f3`
number_of_ids=`echo $ids | wc -l | tr -d '[:space:]'`
for i in {1..$number_of_ids}; do git show `echo $ids | sed -n ${i}p` > evaluate$i.rb;done; 

So this saves all the unreachable objects to files prefixed by "evaluate". I then opened all the files in a text editor (sublime for me subl evaluate*), and evaluated each file in turn, manually copying and pasting the file into the old version of the original file if it was a file from the stash I deleted.

Tips:

  • Change the .rb to match the file extension of the files you are looking for, to get appropriate syntax highlighting if you want it.
  • If your files aren't included, you can widen the scope of your search by removing the | grep blob (the blobs were where I found my files).
-2

Right click on your project in project structure on the left. then select Local History --> Show History check the screen shot.it shows the list of Local Changes left side you can see the corresponding changes at right side.

To apply changes right click on the selected item it gives two options 1)Revert 2)Create Pacth

enter image description here

3
  • This has nothing to do with git, it's a feature in Jetbrains IDEs. May 9, 2022 at 13:42
  • the git way is to fetch all the orphan stash, and git stash apply the one you want. May 9, 2022 at 13:43
  • I actually upvoted this as it ended up being how I recovered most of my files. Even if the question specifies git I don't think this should be discredited as an answer Aug 2, 2022 at 2:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.