DEV Community

Cover image for Recover a lost Git stash in two steps
Mehdi M.
Mehdi M.

Posted on • Edited on • Originally published at blog.mehdi.cc

Recover a lost Git stash in two steps

Sometimes you end up stashing code, then at some point those stashes get cleaned. And one day, you may encounter the situation I faced this week:

Fine, this code is merged, so let’s delete the related stashes. Done! And… hey… wasn’t this part of the feature supposed to be in the codebase? Is the stash I just deleted lost forever?

Fortunately, I managed to recover lost stashes. I’m not a Git expert, but here’s what worked for me after going through various readings (including some Stack Overflow answers).

Here’s the two-steps recovery procedure.

1. List lost stashes

Let’s run this command for a project where all stashes were trashed:

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

It returns a list of lost stashes, ordered by date.
Ho, 3 lost stashes!

  • To quit the list of stashes, press the Q key.
  • To navigate in a long stashes list, use up and down arrows.
  • For Windows user, maybe johnwait’s comment will help you during the battle.

2. Send a lost stash back where it comes from

Let’s use the commit hash of the second stash:

git update-ref refs/stash 4b3fc45c94caadcc87d783064624585c194f4be8 -m "My recovered stash"
Enter fullscreen mode Exit fullscreen mode

And that’s it! You’ll find your stash as usual, using git stash list or by having a look in your favorite Git client.

Gotchas

1. I still can’t see my recovered stash

Retry using the --create-reflog parameter (thanks studoggithub):

git update-ref refs/stash 4b3fc45c94caadcc87d783064624585c194f4be8 --create-reflog -m "My recovered stash"
Enter fullscreen mode Exit fullscreen mode

2. My Git isn’t in English

If your Git isn’t in English, you’ll have to run alias git='LANG=en_GB git' each time you want to recover a set of stashes (thanks mathieuschopfer).

Some advices

Commit messages are healthy

Always use a commit message using git stash save -m "My commit message": without message, the only informations helping to identify a stash are its timestamp and the branch it was saved from, which may not be enough compared to a strong explicit name.

Commit messages also help Git clients:

  • GitUp, the Git client I use, completely fails at showing unnamed stashes. That’s probably why you can’t create a stash in GitUp without giving it a name, which is great!
  • The well-known SourceTree succeeds at showing unnamed stashes, but as you can guess, the list isn’t friendly to browse: Unnamed stashes in SourceTree

Yes, git stash apply > git stash pop

Unlike git stash pop, git stash apply does not remove the stash from the list of stashes, which can avoid some loss.

Branches > stashes

Finally, I’d recommend to avoid git stash. Instead, try to use a branch. This seems obvious but it only comes to me as I was finding a way to recover a stash: maybe I should use temporary branches instead of stashes. Using the Git Flow method at work, this could have come to my mind before encountering a painful experience.

If you have any stash hint or experience that you want to share, comments are welcome.

Latest comments (26)

Collapse
 
krangadurai profile image
Rangadurai

It was incredibly helpful — it saved me an entire month’s worth of work!

Collapse
 
htps611 profile image
htps

Saved my day ❤️

Collapse
 
samar_hussain profile image
Samar Hussain

Mehdi bro you have no idea how big of a trouble you saved me from. I accidentally stashed and cleared hard work of my several days and night (My mistake I shouldn't have piled up that big). And your post just saved my life. I was almost crying. Thanks a lot dude. God bless you.

Collapse
 
meduzen profile image
Mehdi M.

Hey Samar, you know what? I feel there's a lot of gratitude in your message, so I want to say thank you for having taken the time to put it down. It sincerely made my day better (and today is not the easiest one for me 😅).

I hope everything's fine with your work, now. Sharing is caring. ✌️

Collapse
 
pedroverceze profile image
Pedro Verceze

Thanks!!
It is good to know, after the fist command, pick your hash and use:

$ git stash apply e3cf5932f4816f5b0022190ce6b871f51cf882de

It worked for me

Collapse
 
shankarshastri profile image
ShankarShastri

Thanks, it saved my day !!

Collapse
 
mahipalabhijith profile image
Abhijith Mahipal M

Thanks, i got my changes back while my rebase autostash failed

Collapse
 
ericus123 profile image
AMANI Eric

Thanks a lot. This saved me .

Collapse
 
daniaaalarshad profile image
daniaaalarshad

Thanks a lot lot lot lot lot lot. You saved my time and I recovered 70 - 80 percent of my work :)

Collapse
 
mansehej profile image
Mansehej Singh

This was extremely helpful. Thank you for sharing your knowledge!

Collapse
 
zeke profile image
Zeke Hernandez

saved my butt! thanks!