Recovering file changes in VS Code

Here's a trick that saved my backside this week.

On Friday, I hadn't bothered to commit any of my work because I wasn't feeling it. A risky move, but come Monday it was still there. However, for reasons that aren't relevant, I had to revert my local copy of the project to the main branch. Unfortunately, this ended up wiping my uncommitted changes.

Although I'd stashed my changes before switching to the main branch, not even git stash pop could recover them. I did some googling and thankfully, I found this solution (weirdly enough, it wasn't in the VS Code docs).

Here are the steps:

  1. Hold down Command + Shift + P (on MacOS) to open the Command Palette.

  2. Search for "Local History: Find Entry to Restore".

  3. Select or type in which file you want to restore (assuming you remember which ones you edited).

  4. Select the timestamp to which you want to return (N.B. the latest time is not always at the top).

You will then be led to a split screen similar to when you have a merge conflict. You can copy and paste it into your current version of the file.

Et voilà! Hope this rescued you too. Overall, though, let this be a lesson to commit early and often.