Skip to content

Instantly share code, notes, and snippets.

@joseluisq
Last active March 28, 2024 11:59
Show Gist options
  • Save joseluisq/7f0f1402f05c45bac10814a9e38f81bf to your computer and use it in GitHub Desktop.
Save joseluisq/7f0f1402f05c45bac10814a9e38f81bf to your computer and use it in GitHub Desktop.
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 your stash commit is not listed or you don't find it (optional)

If your 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 examine the stash commit diff of your changes.
  • After found your changes just use the corresponding commit of your stash changes and then 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.

@DraganaBelic
Copy link

This was very helpful. Thank you !

@NamNguyenbn
Copy link

You saved my day. Thank u so much!

@robcarrwren
Copy link

saved a days work. cheers!

@jhxxs
Copy link

jhxxs commented Aug 31, 2021

I also use the solutions in this article.
Sadly git update-ref ... or git stash apply [commit-id] didn't work on my pc, git version 2.33.
But i try git reset --hard [commit-id] and it worked.

@rmmitherhofer
Copy link

POH TIOZAO TU SALVOU MINHA VIDA
OH BIG Brow, you saved my life

@Enoch30
Copy link

Enoch30 commented Nov 18, 2021

THANK YOU!

@opotemkin
Copy link

Thanks! it's great

@w33zy
Copy link

w33zy commented Dec 6, 2021

You just saved my life!

@mrnossiom
Copy link

I believed I was going to die! Thank you so much.

@mzvast
Copy link

mzvast commented Mar 9, 2022

It works! You saved my life!

@arraen
Copy link

arraen commented Mar 31, 2022

Thanks a lot! Hours of work were saved. stash apply on commit also don't work for me - fatal: 'dab54e3' is not a stash-like commit, but cherry-pick works well

@beatrizsmerino
Copy link

you saved my life 😍

@gurtej9
Copy link

gurtej9 commented Apr 5, 2022

saved my life..Thanks

@uhhrace
Copy link

uhhrace commented Apr 20, 2022

Incredible

@marcelo-amorim
Copy link

Saved my 3 days of work <3

@chunaixxx
Copy link

thx

@ihatem
Copy link

ihatem commented Dec 8, 2022

Saved my life man, thank you <3

@Emma-Ade
Copy link

Thanks a lot for this!!!

@Vipin1437
Copy link

Thanks a lot for this, It saved my three days work !!!

@mjstanton
Copy link

OMG dude - you saved my sanity and work! Thank you, thank you.

@Kastet85
Copy link

Thanks a lot!

@Kripteks
Copy link

Thank you, you are better than ChatGPT

@wulinjie122
Copy link

wulinjie122 commented Apr 7, 2023

I usually user below command to recover my lost stash:

# get the lost stash and output to find.txt
git fsck --unreachable | awk '{print $3}' | xargs git show >> find.txt

# this command is powerful, you can find your change if you remember the changed content or file name
git log --graph --decorate --pretty=oneline --abbrev-commit --all $(git fsck --no-reflogs | grep commit | cut -d' ' -f3)

# once you get the correct hash, you can get the dropped stash via hash
git stash apply 95ccbd927ad4cd413ee2a28014c81454f4ede82c

@Tolu-Mals
Copy link

Thank you!

@denkhis
Copy link

denkhis commented Jul 4, 2023

Thank you for saving my time 🙏

@jcollum-nutrien
Copy link

git log --graph --oneline --decorate ( git fsck --no-reflog | awk '/dangling commit/ {print $3}' ) will give a zsh: bad pattern error in zshell. I don't recally why that happens. I solved the issue that I had a different way.

@Bizarrus
Copy link

Its work not with lost stashes from 2019 :-(

@PherbCampton
Copy link

I usually user below command to recover my lost stash:

# get the lost stash and output to find.txt
git fsck --unreachable | awk '{print $3}' | xargs git show >> find.txt

# this command is powerful, you can find your change if you remember the changed content or file name
git log --graph --decorate --pretty=oneline --abbrev-commit --all $(git fsck --no-reflogs | grep commit | cut -d' ' -f3)

# once you get the correct hash, you can get the dropped stash via hash
git stash apply 95ccbd927ad4cd413ee2a28014c81454f4ede82c

Thank you, you just saved a life

@mdesai-ontic
Copy link

You saved me

@FranciscoCaldeira
Copy link

FranciscoCaldeira commented Jan 5, 2024

I usually user below command to recover my lost stash:

# get the lost stash and output to find.txt
git fsck --unreachable | awk '{print $3}' | xargs git show >> find.txt

# this command is powerful, you can find your change if you remember the changed content or file name
git log --graph --decorate --pretty=oneline --abbrev-commit --all $(git fsck --no-reflogs | grep commit | cut -d' ' -f3)

# once you get the correct hash, you can get the dropped stash via hash
git stash apply 95ccbd927ad4cd413ee2a28014c81454f4ede82c

If you have this:

error: object hash is a tree, not a commit
fatal: 'hash' is not a stash-like commit

Only use the commit hash not the tree hash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment