Tips & Tricks

Using Local History to Restore Code Fragments or Deleted Files

Continuing our series on how2pro with IntelliJ IDEA, today we’d like to tell you more about a good old feature that can turn out to be a real lifesaver: Local History.

Unlike version control systems, which only keep track of the differences made between commits, local history offers much more. It automatically records your project’s revisions triggered by various events as you edit code, run tests, deploy applications, and so on.

blog banner

Acting as your personal version control system, local history lets you restore deleted files, bring back separate changes, or roll back to any state of a file even if no version control is enabled for your project yet, or if an unwanted change was made after your last commit. It may also serve as a recovery source if your PC restarts unexpectedly before you can take any action.

The default retention period for local history is 5 days, but that’s configurable.

Restore changes

Let’s imagine you had made a ton of changes to a file since your last commit before you realized you’ve deleted a meaningful chunk of code. The undo action can’t help you here, because that change is too far away and you’ll be forced to discard other changes if you use it.

Local history to the rescue! Right-click anywhere in the editor and choose Local History | Show History or press Ctrl+Shift+A on Windows and Linux or ⇧⌘A on macOS and then type ‘local history’.

What you’ll see is a list of all saved revisions of the current file with a timestamp on the left, and the diff viewer on the right which displays the differences between each revision and the current state of the file.

Locate the piece of code you want to restore and simply click the chevron button (») to copy it from the left pane.

local_history_restore_code

Restore deleted files

If you’ve accidentally deleted a file that was not under version control, select the folder that contained that file in the Project view and invoke local history for that folder.

Then select a revision that contains the file you want to restore, right-click that file, and choose Revert Selection:

local_history_restore_file

You can learn more about local history from our documentation.

Local history is available both in IntelliJ IDEA Ultimate and in the free Community Edition. Download IntelliJ IDEA now to make sure you never lose a single line of code!

Happy developing!

image description