VS Code: view Local History & restore previous File Version

avatar
Borislav Hadzhiev

Last updated: Apr 6, 2024
5 min

banner

# Table of Contents

  1. VS Code: view Local History & restore previous File Version
  2. Restoring a previous File version in VS Code
  3. Where Local history files are stored
  4. Finding and restoring files that you've already deleted
  5. Restoring previously deleted files that are not in local history

# VS Code: view Local History & restore previous File Version

VS Code offers built-in local file history with the Timeline feature.

  1. Open a specific file.

  2. Click on TIMELINE in the Sidebar to expand the menu.

expand timeline

Make sure to select a file first and then expand the timeline menu.

Anytime a file is saved, a new entry appears in the Timeline view.

Each local history entry contains the full contents of the file at the time the entry was created.

Here is a short gif that demonstrates how to expand the Timeline view.

expand timeline local history

The timeline panel shows entries containing the file's contents every time it was saved.

timeline entries

You are also able to compare versions of the file in a similar way to a git diff.

compare versions of file

You can right-click on a specific entry to show the available options.

right click on entry

The options are:

  • Compare with File - compare the contents of the entry with the contents of the current file.
  • Compare with Previous - compare with the previous version of the file.
  • Select for Compare - select the other file to compare with the entry.
  • Show Contents - show the contents of this version of the file.
  • Restore Contents - restore the contents of the file based on the selected version.
  • Open Containing Folder - Open the History folder that contains the selected version.
  • Rename - rename the entry.
  • Delete - delete the entry.

If you have a git repository, you will also be able to see remote versions of the file.

You can click on the filter icon if you only want to show only local or remote versions of the file.

filter local history

Note that the Local history filter of the TIMELINE feature only tracks files that you edit in VS Code.

If you edit the file using a different code editor, it won't be tracked and its versions won't get recorded.

I've also written an article on how to view the Git History in VS Code.

# Restoring a previous File version in VS Code

If you need to restore a previous version of a file:

  1. Select the file.
  2. Expand the TIMELINE menu from the sidebar.
  3. Right-click on the version that you'd like to restore.
  4. Click on Restore Contents.

Here is a short clip that demonstrates the process.

restore contents of file

You can also use the command palette to select a file and find an entry to restore.

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type local history and select Local History: Find Entry to Restore.

local history find entry to restore

  1. A new menu is shown in which you can select the file to show local history for.

select file

  1. The last step is to select the local history entry to open.

select local history entry to open

# Where Local history files are stored

The location where local history files are stored depends on your operating system:

On Windows, local history files are stored under:

  • %APPDATA%\Code\User\History

The path resolves to C:\Users\<username>\AppData\Roaming\Code\User\History.

Make sure to replace the <username> placeholder with your actual username.

On macOS, local history files are stored under:

  • $HOME/Library/Application\ Support/Code/User/History.

On Linux, profiles are stored under:

  • $HOME/.config/Code/User/History

You can also find where local history files are stored by:

  1. Expanding the TIMELINE menu from the Side Bar.
  2. Right-clicking on an entry.
  3. Clicking Open Containing Folder.

find folder containing local history files

Once you open the folder that contains the specific entry, go one directory back into the History folder.

# Finding and restoring files that you've already deleted

If you've already deleted the file, it won't be displayed in the TIMELINE menu.

One way to try to restore the file from local history is to use the grep command on macOS or Linux or the findStr command on Windows.

Once you locate the correct version of the file, you can restore it.

# Windows example using findstr

Here is an example that looks for the string bobbyhadz.com using grep on Windows.

cmd
findstr /i /s bobbyhadz.com %APPDATA%\Code\User\History

The /i parameter is used to ignore the case of the characters when searching for the string.

The /s parameter is used to search directories recursively.

# macOS example using grep

Here is an example that looks for the string bobbyhadz.com using grep on macOS.

shell
grep -r -i bobbyhadz.com "$HOME/Library/Application\ Support/Code/User/History"

The -r option is used to search directories recursively.

The -i option is used to perform a case-insensitive search.

# Linux example using grep

Here is an example that looks for the string bobbyhadz.com using grep on Linux.

shell
grep -r -i bobbyhadz.com "$HOME/.config/Code/User/History"

The -r option is used to search directories recursively.

The -i option is used to perform a case-insensitive search.

# Restoring previously deleted files that are not in local history

If you need to restore a deleted file that is not tracked in local history, check the trash bin on your computer.

For example, on Windows:

  1. Open Recycle Bin.
  2. Find the VS Code file that you deleted.
  3. Right-click on the deleted file and click no Restore
  4. The file will get restored to the directory it was deleted from (in your VS Code project).

Alternatively, you can use the VS Code undo functionality.

After you delete a file:

  1. Click on EXPLORER in the left sidebar to focus it.

  2. Click on Edit in the top menu and then click Undo.

Note that you might have to click Alt to show the top menu on Windows and Linux.

edit undo

You can also left-click on explorer and press Ctrl + Z (or Cmd + Z) to restore the deleted file.

However, note that you must focus Explorer first before using the Undo functionality because the main window has a separate stack for Undo.

Here is a short clip that shows how to restore a deleted file using Ctrl + Z to undo.

restore deleted file ctrl z

And, here is a clip that shows how to use the top menu to restore a deleted file.

restore deleted file with undo

I've also written an article on how to undo the last Git commit in VS Code.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.