DEV Community

Krzysztof Platis
Krzysztof Platis

Posted on โ€ข Edited on

150 5 6 6 6

How I restored my deleted file in VSCode: A cool trick you should know ๐Ÿ”ฎ

Hey there! Let me share a crazy experience I had recently. I was working on a lengthy piece of code in a new file in VSCode. After spending hours perfecting it, I accidentally deleted the file before committing it to Git. I only realized the mistake the next day. Total nightmare, right?

The Panic Moment

At first, I was in complete shock. I just stared at my screen, thinking, "Did that really just happen?". My heart started racing, and I felt this wave of panic wash over me. All that hard work, gone. Nothing in the Trash Bin. But then, I started googling whether it was possible to restore a deleted file from VSCode, and I found, to my surprise, a Stack Overflow question where someone in the comments suggested the following cool trick.

The lifesaver: Local History in VSCode

So, there's this feature in VSCode called "Local History: Find Entry to Restore." It basically keeps a history of your files locally, which means you can go back and restore previous versions even if you've deleted them! How cool is that!

How I got my file back

  1. Open command palette

    • First, I opened the Command Palette by pressing Cmd+Shift+P
  2. Find the local history command

    • Then, I pasted the phrase "Local History: Find Entry to Restore" in the Command Palette. It popped up in the list, so I pressed Enter. Image description
  3. Search for my deleted file

    • Next, it asked me to type the name of the file I deleted. I did, and to my happy surprise, my deleted file was there!
  4. Choose the right version

    • After pressing Enter, I was presented with a list of snapshots of my file that VSCode remembered for me. I picked the most recent version and pressed Enter again. Image description
  5. Restore the file

    • Finally, just like that, VSCode restored the contents of my file! It was like magic! I was so happy. Then it sufficed to just save this file contents on my disk with the hotkey Cmd+S and I could commit it now with Git. ๐ŸŽ‰

The Happy Ending

Seeing my code back on the screen was such a relief. I avoided a counter-productive big loss of my work. Not only did I get my work back, but I also learned a super useful trick that I'm now excited to share with you. I hope you'll never need to use it, but... it's better to be prepared ๐Ÿ˜‰

If you really feel like buying me a coffee

... then feel free to do it. Many thanks! ๐Ÿ™Œ

Buy Me A Coffee

Image of Timescale

๐Ÿš€ pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applicationsโ€”without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post โ†’

Top comments (25)

Collapse
 
laurent_ionesco_a77425eac profile image
Laurent Ionesco โ€ข

It also works if you worked on a remote server. You opened files through Winscp, you edited them in VS Code (it opened and saved them in a temp directory). If something nasty happened on the server and you overwrote a file, you can recover it with the method here. Thanks Krzysztof!

Collapse
 
krisplatis profile image
Krzysztof Platis โ€ข

Wow. Awesome you're sharing it! Thanks for info!

Collapse
 
e1bos profile image
Luca โ€ข

I created an account just to thank you.

I had an assignment with a controller file with over 500 lines of code, and I was helping my professor debug a PHP setup error and forgot to rename the dummy file. All my code was gone, I was devastated, went through total shock.

You saved my assignment, my time, and my mental health. Thank you for writing this, for sharing this. I wish the utter best for you.

Collapse
 
krisplatis profile image
Krzysztof Platis โ€ข

I'm happy it worked for you! ๐Ÿ™Œ

Collapse
 
senthilkumaranc profile image

I have lost of all my files. It got Permanently deleted. Unable to recover from anywhere.

Finally able to recover all files because of this post.

Thanks to you and also to my brother Arvind who pointed out this post.

Thanks to VS code team too.

Collapse
 
alexantoshuk profile image
alexantoshuk โ€ข

I accidentally did a git checkout but forgot to commit, all the files were replaced by the old version. Local History helped me restore everything! Thank you!

Collapse
 
sayidina_ahmadalqososyi_ profile image
Sayidina Ahmadal Qososyi โ€ข

thanks :")

Collapse
 
abhishek_waghmare profile image
Abhishek Waghmare โ€ข

Thank you, you just saved my 2 weeks of work!

Collapse
 
hashim_hussain_5 profile image
Hashim Hussain โ€ข

Amazing it works perfectly thanks for info.

Collapse
 
ayyash profile image
Ayyash โ€ข

you could have just opened the trash bin and restored :) but yes this is easier

Collapse
 
krisplatis profile image
Krzysztof Platis โ€ข

Good idea. I've tried it too, but the file wasn't in the trash. Likely, because I've accidentally removed it with some git command (but not regular "delete file" action in the system).

Collapse
 
amtul__sadiya_ec9c2c3e707 profile image
amtul_ sadiya โ€ข โ€ข Edited

I just wanted to say a huge thank you for your post. I messed up my project and thought I lost everything, but your solution saved me! I canโ€™t tell you how relieved I am.
thanks Krzysztof! ๐Ÿ™Œ๐Ÿ™Œ

Collapse
 
krisplatis profile image
Krzysztof Platis โ€ข

Thanks for sharing your feelings! I can totally feel for you and could feel the same... twice ๐Ÿ˜… - it happened to me once again a few months later after sharing this blogpost that I deleted my files once again ๐Ÿ™ˆ)
By the way, just in case you'd like share your joy in other ways too, you might want to buy me a coffee: buymeacoffee.com/krisplatis ๐Ÿ˜‰

Collapse
 
wuyen profile image
Wuyen โ€ข

Thank you , you saved my life

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs