Recover Delete Files / Folders With PHPStorm

Today I want to share with you a way in which you can recover deleted files and / or folders inside PHPStorm, even if you did something like an rm -rf *, or git reset HEAD --hard.

There is a fantastic piece of functionality built right into PHPStorm called the Local History, which - as the name implies - keeps a local (to your user profile) diff of all changes to your files and folders.

The reason I want to share this is two fold.

Firstly, knowing about this in advance can save you a lot of time / effort / stress.

Secondly, it is genuinely useful.

Oops, I Just Lost An Afternoon's Work

Just last week I found myself working on a project at an enterprise client. Windows is largely the order of the day, and SVN is still the version control system of choice on a number of their projects.

I happened to be working on one such project.

I was working on a new feature for the project. I had branched off, made a few commits and all was good.

Mid way through, I was asked to amend the config.yml file for the code currently in production.

No matter, you would think, save / commit changes, switch branches, do the job, nothing to concern yourself with.

I commit my changes (or so I thought), then branched off from the master branch and went about my business. After completing the task I went to do a build. A bunch of my files from the other working branch still remained on the current branch - and that was making the build fail.

I did the sensible thing - delete the offending files (after wondering why they were still there) - and got the build to complete successfully.

The next morning I went to continue with my original feature.

Switching branches went ok, but I was 5 files short.

You guessed it - those 5 files I had forcefully deleted yesterday to make the build pass.

Of the files, 3 were PHPUnit test files, and two were implemented logic.

All in, it represented a loss of an afternoon's work. Panic.

Here I was, about to have to tell my client that I had made a mistake that would make me look like a noob at best, and incompetant at worse.

I searched everywhere - on my build server, on the SVN repo, in my recycle bin, a full disk search for the file names... but nothing. They were definitely gone.

PHPStom To The Rescue

There are many solutions to this problem.

The most obvious one is to learn how to SVN properly.

The second most obvious one is - switch to git, please! :)

A frustrating response to this problem may be : "take regular backups"

It's good advice, and I do back up, but even the Windows on disk back up thing that it does only had my folders from 2 days prior. No use at all.

All of these things are fine if you learn your lessons (even the hard way) and then implement a solution now, so that you don't get (inevitably) stung again in the future.

PHPStorm, however, has a solution that already has your back.

It's worth noting that this solution applies to PHPStorm, WebStorm, PyCharm, or any of the other IntelliJ-based IDEs.

Built right into PHPStorm is the concept of Local History.

Local History can be found by going to VCS on the top bar, and then Local History > Show History.

It is contextual, so if you highlight the top level folder of your project, you see all the history for all changes in your folder structure.

If you highlight a single file, then repeat the process, or simply right click and select Local History > Show History then you can see the individual changes to a particular file.

In my case, having deleted files and folders that didn't exist until I created them, I just had to go one folder level up, see the history, then right click each file and restore.

What blows my mind about this is: I didn't delete the files inside PHPStorm, but it caught the changes anyway.

Simply amazing.