Skip to content

Instantly share code, notes, and snippets.

@ben-alkov
Created September 25, 2025 19:41
Show Gist options
  • Select an option

  • Save ben-alkov/8645615149d0b017140cd932bb73a269 to your computer and use it in GitHub Desktop.

Select an option

Save ben-alkov/8645615149d0b017140cd932bb73a269 to your computer and use it in GitHub Desktop.
Git: recover a dropped stash (or any commit ever made)

Git: recover a dropped stash (or any commit ever made)

Finding the hash

If you have only just popped it and the terminal is still open, you will still have the hash value printed by git stash pop on screen (thanks, Dolda).

Otherwise, you can find this way in Linux, Unix or Git Bash for Windows

git fsck --no-reflog | awk '/dangling commit/ {print $NF}'

… or in PowerShell for Windows:

git fsck --no-reflog | select-string 'dangling commit' | foreach { $_.ToString().Split(" ")[-1] }

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.

The easiest way to find the stash commit you want is probably to pass that list straight to gitk:

gitk --all $( git fsck --no-reflog | awk '/dangling commit/ {print $NF}' )

… or in PowerShell for Windows:

gitk --all $( git fsck --no-reflog | select-string 'dangling commit' | foreach { $_.ToString().Split(" ")[-1] } )

This will launch a repository browser showing you every single commit in the repository ever, regardless of whether it is reachable or not.

You can replace gitk there with something like git log --graph --oneline --decorate if you prefer a nice graph on the console over a separate GUI app, e.g.

  *   5bf99d58 On main: claude vs. pulp
  |\
  | * 47c09bdd index on main: 1aebd116 yarn: Move SemverLike type alias to type_aliases
  |/
  | * 224e42bf On (no branch): claude: yarn v4 patches
  |/|
  | * b4042940 index on (no branch): 1aebd116 yarn: Move SemverLike type alias to type_aliases
  |/
  | * bca5e4d3 test(yarn v4): project-root patches should be supported
  |/
  | * 1d485fa7 fix(yarn v4): project-root patches without workspace locators
  | * f8e835d9 test(yarn v4): project-root patches should be supported
  |/
  | * e4939900 test(yarn v4): project-root patches

To spot stash commits, look for commit messages of this form:

WIP on somebranch: COMMITHASH Some old commit message

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.

Once you know the hash

Apply it as a stash:

git stash apply $stash_hash

Or, you can create a separate branch for it with

git branch recovered $stash_hash

After that, you can do whatever you want with all the normal tools. When you’re done, just blow the branch away.

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