Local History in IntelliJ IDEA May Save Your Life Code

Posted on by Anna Gasparyan

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!

Comments below can no longer be edited.

3 Responses to Local History in IntelliJ IDEA May Save Your Life Code

  1. Jerzy says:

    February 10, 2020

    I had lot’s of newly created files staged for commit when I issued a git reset --hard. I did not know this would delete the files in the staging area. But it did, and the local history feature helped me to recover.

    There might be a way to recover git’s staging area, but it would be obscure.

  2. David Nugent says:

    February 10, 2020

    This is one of those features that you may not use often, but when you do it is a godsend.
    Kudos for the article. This and similar features is one of the reasons I continue to support Jetbrains products.

  3. John Ulric says:

    February 11, 2020

    Very useful feature indeed, but unfortunately, the local history is tied to the caches. So, when you have to invalidate your caches, you also lose your local history. Indexing and inspections have been somewhat fragile in the latest releases, so I had to use “Invalidate Caches and Restart” quite a lot.

Subscribe

Subscribe for updates