Recover a dropped stash in git

I was working on a git branch and did a 'git stash save' on some changes, then did a pull, then went to lunch. When I got back, I fixed something on another branch, then went to 'git stash pop' them.

Without thinking, I did a hard reset and wiped them out, but I'd already taken them out of the stash area. Ack!

I thought about checking to see if it's possible to recover a dropped stash from the reflog, but turns out it was easier than that. I had already run git stash show earlier and had something like this:

$ git stash pop
        [...]
        Dropped refs/stash@{0} (6ddc4021719ddd8f5e35f5d63e8e04b9ab2c5650)
        

In this case, I had the SHA of that commit, so a git branch tmp 6ddc4 made a new branch with that commit on the tip of it, and I could move along.

Naturally, I couldn't stop thinking about this and wondered what I would do if I didn't have the commit around. Could I find it in the reflog?

Turns out, you can use git fsck to find 'dangling commits':

$ git fsck --no-reflog | grep 'dangling commit' | cut -d ' ' -f 3
        
This gave me a list of all commits that weren't referenced anymore. From there, I did git show SHA on them to find the one I was interested in.

Established 2005 · Databasically © 2016