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

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

@leonidb

This comment has been minimized.

Copy link

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 commented Mar 22, 2019

Thanks!

@nbhaohao

This comment has been minimized.

Copy link

nbhaohao commented May 14, 2019

Thank you! save me successfully.

@roychri

This comment has been minimized.

Copy link

roychri commented May 16, 2019

THANK YOU SO MUCH! <3

@Robert-Beier

This comment has been minimized.

Copy link

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 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 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 commented Jun 26, 2019

You just saved my code. Thanks so much!

@DRAMWATER

This comment has been minimized.

Copy link

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 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 commented Jul 4, 2019

Thank you! save me successfully.

@aleksft

This comment has been minimized.

Copy link

aleksft commented Jul 23, 2019

THANK YOU!!

@atmonshi

This comment has been minimized.

Copy link

atmonshi commented Jul 29, 2019

❤️

@at-huypham

This comment has been minimized.

Copy link

at-huypham commented Aug 4, 2019

You saved my day. Thank you!!

@vhwr

This comment has been minimized.

Copy link

vhwr commented Aug 16, 2019

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

@crtjr64

This comment has been minimized.

Copy link

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 commented Aug 26, 2019

You saved my 6 hrs of work thanks to all 👍

@pavangopal

This comment has been minimized.

Copy link

pavangopal commented Sep 4, 2019

Worked for me. Thank you so much.

@Arnie97

This comment has been minimized.

Copy link

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 commented Sep 20, 2019

+1

@crossan007

This comment has been minimized.

Copy link

crossan007 commented Sep 23, 2019

Thanks!! This saved my 🥓

@jordangarvey

This comment has been minimized.

Copy link

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 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!

@PritamJ

This comment has been minimized.

Copy link

PritamJ commented Oct 15, 2019

Worked ❤️ 🎉

@vaivaswatha

This comment has been minimized.

Copy link

vaivaswatha commented Oct 17, 2019

Worked 👍 thanks a lot

@eduardoabpereira

This comment has been minimized.

Copy link

eduardoabpereira commented Oct 18, 2019

Thanks! Saved me a lot!!!!!

@AnimeMei

This comment has been minimized.

Copy link

AnimeMei commented Oct 29, 2019

+2

@Vict0r1

This comment has been minimized.

Copy link

Vict0r1 commented Nov 13, 2019

Thanks!

@mousemckill

This comment has been minimized.

Copy link

mousemckill commented Nov 18, 2019

Thanks!

@RidhwanDev

This comment has been minimized.

Copy link

RidhwanDev commented Nov 18, 2019

THANK YOU!

@seattaken

This comment has been minimized.

Copy link

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.

@bartnic1

This comment has been minimized.

Copy link

bartnic1 commented Jan 2, 2020

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

@shwetasgoyal

This comment has been minimized.

Copy link

shwetasgoyal commented Jan 10, 2020

Thank you so much.. saved me totally!

@Xstream007

This comment has been minimized.

Copy link

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 commented Feb 6, 2020

You save my life. lv it.

@brunohccz

This comment has been minimized.

Copy link

brunohccz commented Feb 20, 2020

You save my life man!!! Thank you

@cupertank

This comment has been minimized.

Copy link

cupertank commented Mar 17, 2020

THANK YOU SO MUCH <3 <3 <3

@metalg0su

This comment has been minimized.

Copy link

metalg0su commented Mar 24, 2020

YOU SAVE MY LIFE.

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.