<-- go back

How to Recover Files From a Deleted Stash

VS Code decided to trick me with ghost files leading me to erase my progress—or so I thought.

 — 2 min read

Bug


I was stashing and popping with git using VS Code (not through the CLI) when all of a sudden I just lost all my work. omg

It was some kind of display bug where I stashed all my work. On the source control window, there were still files there, but also there was a new entry in my stash list. And so I decided to drop the newly added stash to try to stash again.

This time, nothing happened. There were still unstaged files in the window. I did git status and nothing showed up. Everything was gone. I don't understand why did VS Code still show unstaged files, but when I check the status on the CLI, nothing shows up?

VS Code decided to lie to me making me believe that my files were still present.

Because of this, I frantically looked everywhere for a solution going so far as to observe the VS Code logs to see the git commands being executed by VS Code as I click on the buttons.

I read through this GitHub issue where the one of the contributors said that they don't log the output of the git commands. Ok.

Fix


This Stack Overflow thread saved me. Most of the solutions didn't apply to me however except for one which I present here.

Prerequisites

You must have gitk installed which graphically displays a git repository. This was super helpful in looking through each hash.

On MacOS:

brew install git-gui

Recovering Your Files

Interestingly, I learned that when you stash things, you are also making a commit. And when you pop or drop a commit, it seems to be unreferencing it as opposed to just deleting it from existence.

To view and examine all commits ever made in the repository, including those from stashes that were popped or dropped, run the command below.

This utilizes git fsck to find dangling commits and gitk to visually inspect these commits:

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