Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to recover a dropped stash in Git?

How to recover a dropped stash in Git?

1. Find the stash commits

git log --graph --oneline --decorate ( git fsck --no-reflog | awk '/dangling commit/ {print $3}' )

This will show you all the commits at the tips of your commit graph which are no longer referenced from any branch or tag – every lost commit, including every stash commit you’ve ever created, will be somewhere in that graph.

bash/sh shell users: Version above is for Fish shell, so if you are Bash/SH user just add a $ sign before to the left parenthesis.

2. Once you know the hash of the commit you want, you can apply it as a stash

git stash apply YOUR_WIP_COMMIT_HASH_HERE

Note: The commit message will only be in this form (starting with "WIP on") if you did not supply a message when you did git stash.

Source: View the complete answer at https://stackoverflow.com/a/91795/2510591

3. If you stash commit is not listed or you don't find it (optional)

If you stash was already applied but you don't see it, for example after resolving a conflict or reset. Follow these steps:

  • Run git fsck --no-reflog | awk '/dangling commit/ {print $3}'
  • Pick a stash commit hash and use git show COMMIT_HASH in order to explore the stash commit diff of your changes.
  • Identify the commit of your stash changes and just apply it using git stash apply COMMIT_HASH

Bonus

If you are Fish shell user, you can take a look at GitNow which is a tool to perform faster Git operations and that can also stash your changes for you.

@leonidb

This comment has been minimized.

Copy link

@leonidb leonidb commented Dec 10, 2018

I think this command is missing a $ before ( git fsck...:

git log --graph --oneline --decorate $( git fsck --no-reflog | awk '/dangling commit/ {print $3}' )
@thai-dn

This comment has been minimized.

Copy link

@thai-dn thai-dn commented Mar 22, 2019

Thanks!

@nbhaohao

This comment has been minimized.

Copy link

@nbhaohao nbhaohao commented May 14, 2019

Thank you! save me successfully.

@roychri

This comment has been minimized.

Copy link

@roychri roychri commented May 16, 2019

THANK YOU SO MUCH! <3

@Robert-Beier

This comment has been minimized.

Copy link

@Robert-Beier Robert-Beier commented May 20, 2019

I was clicking too fast. You just saved me. Thank you!
This works on powershell:

git log --graph --oneline --decorate $( git fsck --no-reflog | %{ $_.Split(' ')[2]; } )
@iSkore

This comment has been minimized.

Copy link

@iSkore iSkore commented Jun 19, 2019

You just saved me hours and hours of remaking code I accidentally deleted.
THANK YOU SO MUCH

@cesardka

This comment has been minimized.

Copy link

@cesardka cesardka commented Jun 25, 2019

Dios mio, you just saved me after an accidental git stash drop
Just had to add a $ right before the ( that precedes git fsck and there my lost stash was!

Thank you!!

@ocke

This comment has been minimized.

Copy link

@ocke ocke commented Jun 26, 2019

You just saved my code. Thanks so much!

@DRAMWATER

This comment has been minimized.

Copy link

@DRAMWATER DRAMWATER commented Jun 30, 2019

$ git fsck --graph --online --decorate $(git fsck --no-reflog | awk '/dangling commit/{print $3}')
Checking object directories: 100% (256/256), done.
Checking objects: 100% (1990/1990), done.
error: unknown option `graph'
usage: git fsck [] [...]

I get this error ??

@DRAMWATER

This comment has been minimized.

Copy link

@DRAMWATER DRAMWATER commented Jun 30, 2019

My Story - 1.Stashed my changes
2. Pulled the latest from git
3.Deleted my repo from local .
I had 2 weeks of work in my stash and all that is gone now .. will this command help me ?

@yanlee26

This comment has been minimized.

Copy link

@yanlee26 yanlee26 commented Jul 4, 2019

Thank you! save me successfully.

@aleksft

This comment has been minimized.

Copy link

@aleksft aleksft commented Jul 23, 2019

THANK YOU!!

@atmonshi

This comment has been minimized.

Copy link

@atmonshi atmonshi commented Jul 29, 2019

❤️

@at-huypham

This comment has been minimized.

Copy link

@at-huypham at-huypham commented Aug 4, 2019

You saved my day. Thank you!!

@floridene

This comment has been minimized.

Copy link

@floridene floridene commented Aug 16, 2019

I can just agree with the others: Thank you soo much!!

@crtjr64

This comment has been minimized.

Copy link

@crtjr64 crtjr64 commented Aug 22, 2019

This doesn't work for me. I get an error message stating git is not a cmdlet. I am using VS 2019 with Azure DataOps that forced git instead of TFS and I dropped stash by mistake. I thought it would drop a selected file from the stash. But every version of powershell I try to use comes back with this same message. Even when I find a folder with a git command, powershell says it cannot run it:


git : The term 'git' is not recognized as the name of a cmdlet, function, script file, or operable program. Check 
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ git log --graph --oneline --decorate
+ ~~~
    + CategoryInfo          : ObjectNotFound: (git:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

@hbothra15

This comment has been minimized.

Copy link

@hbothra15 hbothra15 commented Aug 26, 2019

You saved my 6 hrs of work thanks to all 👍

@pavangopal

This comment has been minimized.

Copy link

@pavangopal pavangopal commented Sep 4, 2019

Worked for me. Thank you so much.

@Arnie97

This comment has been minimized.

Copy link

@Arnie97 Arnie97 commented Sep 17, 2019

@DRAMWATER

$ git fsck --graph --online --decorate $(git fsck --no-reflog | awk '/dangling commit/{print $3}')
error: unknown option `graph'
usage: git fsck [] [...]

I get this error ??

Well, it should be

$ git log --graph ...

not

$ git fsck --graph ...

Hopes I'm not too late ;-)

@denniscastro

This comment has been minimized.

Copy link

@denniscastro denniscastro commented Sep 20, 2019

+1

@crossan007

This comment has been minimized.

Copy link

@crossan007 crossan007 commented Sep 23, 2019

Thanks!! This saved my 🥓

@jordangarvey

This comment has been minimized.

Copy link

@jordangarvey jordangarvey commented Oct 9, 2019

This genuinely saved me a couple of days work 😅 never going that long without committing again

@EHJ-52n

This comment has been minimized.

Copy link

@EHJ-52n EHJ-52n commented Oct 11, 2019

Just want to say thank you. The original version did not work but the following as outline in #2781737:

git log --graph --oneline --decorate $( git fsck --no-reflog | awk '/dangling commit/ {print $3}' )

Versions:

  • git: 2.17.1
  • os: Ubuntu 18.04.3 LTS
  • zsh: 5.4.2

Have a nice day @ALL!

@vaivaswatha

This comment has been minimized.

Copy link

@vaivaswatha vaivaswatha commented Oct 17, 2019

Worked 👍 thanks a lot

@eduardoabpereira

This comment has been minimized.

Copy link

@eduardoabpereira eduardoabpereira commented Oct 18, 2019

Thanks! Saved me a lot!!!!!

@zmei95

This comment has been minimized.

Copy link

@zmei95 zmei95 commented Oct 29, 2019

+2

@Vict0r1

This comment has been minimized.

Copy link

@Vict0r1 Vict0r1 commented Nov 13, 2019

Thanks!

@mousemckill

This comment has been minimized.

Copy link

@mousemckill mousemckill commented Nov 18, 2019

Thanks!

@RidhwanDev

This comment has been minimized.

Copy link

@RidhwanDev RidhwanDev commented Nov 18, 2019

THANK YOU!

@seattaken

This comment has been minimized.

Copy link

@seattaken seattaken commented Dec 18, 2019

i wrote a long message seeking help but now deleted as based on your description, i have been able to recover my code back. its such a relief. God bless you.

@bartnic2

This comment has been minimized.

Copy link

@bartnic2 bartnic2 commented Jan 2, 2020

Thank you...saved me so much time!!

@shwetasgoyal

This comment has been minimized.

Copy link

@shwetasgoyal shwetasgoyal commented Jan 10, 2020

Thank you so much.. saved me totally!

@Xstream007

This comment has been minimized.

Copy link

@Xstream007 Xstream007 commented Jan 14, 2020

What helped is the following process:-

  1. Using given command in the folder where my project resides:
    git log --graph --oneline --decorate $( git fsck --no-reflog | awk '/dangling commit/ {print $3}' )

  2. It provided me following details:
    image

  3. Stash ID I needed was 4ab02fa which I found on the top.

  4. Executed following command:
    git show 4ab02fa
    image

  5. Used the SHA commit code and did following and it worked:
    git stash apply <SHA commit code>

It was really helpful. Saved lot of my work. Thank you guys.

@nimantha-lakshan

This comment has been minimized.

Copy link

@nimantha-lakshan nimantha-lakshan commented Feb 6, 2020

You save my life. lv it.

@brunohccz

This comment has been minimized.

Copy link

@brunohccz brunohccz commented Feb 20, 2020

You save my life man!!! Thank you

@cupertank

This comment has been minimized.

Copy link

@cupertank cupertank commented Mar 17, 2020

THANK YOU SO MUCH <3 <3 <3

@metalg0su

This comment has been minimized.

Copy link

@metalg0su metalg0su commented Mar 24, 2020

YOU SAVE MY LIFE.

@nishu112

This comment has been minimized.

Copy link

@nishu112 nishu112 commented Apr 3, 2020

Really saved my day!!

@edwinchenloo

This comment has been minimized.

Copy link

@edwinchenloo edwinchenloo commented Apr 4, 2020

a git stash today somehow reverted my code to what was last pushed. I thought i lost several days of work. Your steps allowed me to recover what i had. Thank you!

@estyfen

This comment has been minimized.

Copy link

@estyfen estyfen commented Apr 15, 2020

This doesn't work for me. I get an error message stating git is not a cmdlet. I am using VS 2019 with Azure DataOps that forced git instead of TFS and I dropped stash by mistake. I thought it would drop a selected file from the stash. But every version of powershell I try to use comes back with this same message. Even when I find a folder with a git command, powershell says it cannot run it:


git : The term 'git' is not recognized as the name of a cmdlet, function, script file, or operable program. Check 
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ git log --graph --oneline --decorate
+ ~~~
    + CategoryInfo          : ObjectNotFound: (git:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Hi! , Can you help me by indicating if you could solve this case? and how did you do I also use Visual Studio 2019 git. Thanks!! @crtjr64

@crtjr64

This comment has been minimized.

Copy link

@crtjr64 crtjr64 commented Apr 15, 2020

This doesn't work for me. I get an error message stating git is not a cmdlet. I am using VS 2019 with Azure DataOps that forced git instead of TFS and I dropped stash by mistake. I thought it would drop a selected file from the stash. But every version of powershell I try to use comes back with this same message. Even when I find a folder with a git command, powershell says it cannot run it:


git : The term 'git' is not recognized as the name of a cmdlet, function, script file, or operable program. Check 
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ git log --graph --oneline --decorate
+ ~~~
    + CategoryInfo          : ObjectNotFound: (git:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Hi! , Can you help me by indicating if you could solve this case? and how did you do I also use Visual Studio 2019 git. Thanks!! @crtjr64

Unfortunately I didn't resolve it. I lost that bit of code and I work around stashing. What I did do was save all the code manually to another folder zipped it and created a new branch. Now I use that branch and don't use stash. If you have the code it is my udnerstanding you can request in directly but as I said, it never worked for me. Sorry.

Also, I create my projects on Azure DevOps first and not from Visual Studio 2019 because it defualts to GIT. When I create the project on Azure the option is there to choose TFS

@estyfen

This comment has been minimized.

Copy link

@estyfen estyfen commented Apr 15, 2020

Hi @crtjr64

I could already find how to do it. Commands can be run, using Git Bash on windows and using the $ character before the parenthesis.

git log --graph --oneline --decorate $( git fsck --no-reflog | awk '/dangling commit/ {print $3}' )

imagen

Tutorial Git Bash, Bash basics Here

Cheers

@Dunkalunk

This comment has been minimized.

Copy link

@Dunkalunk Dunkalunk commented Apr 16, 2020

Hoooooooooly crap thank you so much!!

Stashed 2 days worth of changes and my OCD said "get rid of that other stash in the list" so naturally I hit CLEAR!! Glad I found it with this!

@AniketMalik

This comment has been minimized.

Copy link

@AniketMalik AniketMalik commented May 2, 2020

Thanks...works like charm

@cproensa

This comment has been minimized.

Copy link

@cproensa cproensa commented May 5, 2020

👍

@jorge-saludos

This comment has been minimized.

Copy link

@jorge-saludos jorge-saludos commented May 13, 2020

So happy to have this solution!
Thanks a lot!

@isotopeee

This comment has been minimized.

Copy link

@isotopeee isotopeee commented May 27, 2020

Saved my day!!! Thanks!

@FAlexandrou97

This comment has been minimized.

Copy link

@FAlexandrou97 FAlexandrou97 commented May 29, 2020

Thanks!!!!

@aka8921

This comment has been minimized.

Copy link

@aka8921 aka8921 commented Jun 9, 2020

thanks a ton !!!!!

@losch-ap

This comment has been minimized.

Copy link

@losch-ap losch-ap commented Jun 9, 2020

Saved me hours. Thank you!

@ARIPRASATH4664

This comment has been minimized.

Copy link

@ARIPRASATH4664 ARIPRASATH4664 commented Jun 16, 2020

I think this command is missing a $ before ( git fsck...:

git log --graph --oneline --decorate $( git fsck --no-reflog | awk '/dangling commit/ {print $3}' )

Thanks

@wrg20

This comment has been minimized.

Copy link

@wrg20 wrg20 commented Jun 25, 2020

Saved my life. Thanks!

@ludohenin

This comment has been minimized.

Copy link

@ludohenin ludohenin commented Jul 2, 2020

❤️

@jasonb-medopad

This comment has been minimized.

Copy link

@jasonb-medopad jasonb-medopad commented Jul 6, 2020

One million claps 👏

@akseyh

This comment has been minimized.

Copy link

@akseyh akseyh commented Jul 7, 2020

thanks +1

@Moejoe90

This comment has been minimized.

Copy link

@Moejoe90 Moejoe90 commented Jul 15, 2020

👍

@Dugo03

This comment has been minimized.

Copy link

@Dugo03 Dugo03 commented Jul 16, 2020

This guy is a Saint. Saved me too.

@kalpabarbosa

This comment has been minimized.

Copy link

@kalpabarbosa kalpabarbosa commented Aug 21, 2020

Excelente!

@chncwang

This comment has been minimized.

Copy link

@chncwang chncwang commented Aug 22, 2020

Thank you!

@jaeky

This comment has been minimized.

Copy link

@jaeky jaeky commented Aug 31, 2020

THANK YOU 👍 👍

@roblima92

This comment has been minimized.

Copy link

@roblima92 roblima92 commented Aug 31, 2020

One work's week saved with these instructions. THANKS A LOT!!

@YuriBispo

This comment has been minimized.

Copy link

@YuriBispo YuriBispo commented Oct 7, 2020

You've just saved me 2 work days with this. Thank you SO MUCH!

@hui61

This comment has been minimized.

Copy link

@hui61 hui61 commented Oct 12, 2020

Save me, thank you !

@R3m0ved

This comment has been minimized.

Copy link

@R3m0ved R3m0ved commented Oct 12, 2020

Saved me too, also thx @estyfen for the Win tip

@lfachinetto

This comment has been minimized.

Copy link

@lfachinetto lfachinetto commented Oct 19, 2020

Thanksss!

@jediyozh

This comment has been minimized.

Copy link

@jediyozh jediyozh commented Oct 25, 2020

Saved my life!

@matejcsok-istvan

This comment has been minimized.

Copy link

@matejcsok-istvan matejcsok-istvan commented Nov 4, 2020

best page I have ever seen on the whole internet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.