How to recover a dropped stash in Git?

Greg Hewgill

I frequently use git stash and git stash pop to save and restore changes in my working tree. Yesterday I had some changes in my working tree that I had stashed and popped, and then I made more changes to my working tree. I'd like to go back and review yesterday's stashed changes, but git stash pop appears to remove all references to the associated commit.

I know that if I use git stash then .git/refs/stash contains the reference of the commit used to create the stash. And .git/logs/refs/stash contains the whole stash. But those references are gone after git stash pop. I know that the commit is still in my repository somewhere, but I don't know what it was.

Is there an easy way to recover yesterday's stash commit reference?

Note that this isn't critical for me today because I have daily backups and can go back to yesterday's working tree to get my changes. I'm asking because there must be an easier way!

Aristotle Pagaltzis

Once you know the hash of the stash commit you dropped, you can 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.

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 it using this for Linux, Unix or Git Bash for Windows:

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

...or using Powershell for Windows:

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

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 to gitk:

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

...or see the answer from emragins if using Powershell for Windows.

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.

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to recover from "git stash save --all"?

From Java

How to delete a stash created with git stash create?

From Java

How to Git stash pop specific stash in 1.8.3?

From Dev

Git: How to get stash before the last stash?

From Dev

How to recover a commit in git

From Dev

Django 1.7 - Accidentally Dropped One Table. How To Recover It?

From Dev

I dropped brigthness to 0 in compizconfig. How to recover

From Dev

How to undo git stash clear

From Dev

How to list the parent commit of a stash in `git stash list`

From Dev

Git: how to use stash -p to stash specific files?

From Dev

How to git stash pop the last stash created on current branch?

From Java

How can I rename a git stash?

From Java

How can I git stash a specific file?

From Dev

how to move git commits to and from the stash?

From Dev

How do I undo a partial git stash?

From Dev

How to git fetch by branch creator from stash

From Dev

GitPython -- How to 'git stash' changes to a GitPython repository?

From Dev

How to git stash only untracked files?

From Dev

How can I share a git stash?

From Dev

Git - how to stash away a conflict and resolve it later

From Dev

git stash reporting: '' is not a stash reference

From Dev

How do I recover a deletion in git?

From Dev

How to recover deleted .git/modules folder

From Dev

Files deleted after rebase with git, how to recover?

From Dev

How to recover files added to git but overwritten by checkout

From Dev

How do I recover files using git?

From Dev

How to recover broken/partially deleted git repository

From Dev

How to recover files added to git but overwritten by checkout

From Dev

Git Repository How to recover deleted files?

Related Related

HotTag

Archive