In the last article, we looked at comparing things in Git. In this series of articles, we'll look at ways to compare things in the PhpStorm development environment.
If you're a regular reader of my blog, you know that I'm fond of my code editor, PhpStorm. I've been using PhpStorm for years, but I'm still discovering features that I didn't know about. Many of them are tremendous time savers. It's clear that the people who write code for PhpStorm are doing it *in* PhpStorm. In this and the following articles, we'll look at the small, but tremendously useful, part of PhpStorm that let's you compare things.
In the last article, we talked about comparing things in Git. PhpStorm has an integrated Version Control System (VCS) that piggybacks on whatever VCS you use (e.g., Git, Mercurial, or SVN). To be honest, I've never used it because I don't like anything getting in between me and Git. I use the Terminal tab in PhpStorm to shell out to the Cmder (after configuring PhpStorm to use it as my terminal), which is a jazzed-up version of the windows command line (complete with color highlighting, cut-and-paste, and the ability to hit the up arrow to see the previous command, edit it, then issue it again). From there, I can work with Git, Composer, or any other installed tool from the command line.
All that said, the built-in VCS does have some nice features. With one click and no typing, it will show you the equivalent of
git status, or
git branch. It will also do
git stash and
git stash apply, and keep track of the stashes for you.
There are some comparison features in PhpStorm that have nothing to do with Git or any VCS. In this and the following articles, we'll look at some of them.
Comparing Versions of a File
One of the most common things you want to do is compare a file with previous versions of itself. You can do this directly in Git, but it's not very convenient and Git organizes things by commits, so if you don't commit very often, you won't have a very detaile record. Also, in Git there's no way to revert just some of the changes to a file in a single commit and not others. PhpStorm does it by saves, which gives you a much finer-grained look at the changes, and lets you revert individual changes you made between saves. The tool you want is called Local History.
Say you've done a bunch of work in various parts of a large file, but haven't saved it. You want to revert one of your changes but you can't remember where it is. Local History is just the feature you need.
Local History has saved my butt more times than I can count. You can get to it by right-clicking on a file in the tree and selecting "Local History," or you can see the local history of the current file you have open in the editor by choosing that option on the VCS menu (whether or not you have a Version Control system installed). This feature is built-in and has nothing to do with Git or any other VCS.
Have you ever made a series of changes to a file, and a few hours later, after doing a bunch of other work on the file, realized that your earlier changes were stupid? This happens to me all the time. I think of a more efficient way of doing things, for example, and make a bunch of changes to the file. An hour or two later, after a little testing, I discover that my new method has a fatal flaw and I need to undo my work on that method, without messing up the other work I've done on the file.
Every time you save a file in PhpStorm, it saves the changes from the previous save. When you select "Local History," you see a list on the left of each set of changes, labeled with date and time markers like "33 minutes ago," or "Yesterday 11:20 pm." By default, the record only goes back 5 days from the current time, but that's usually enough. The setting is configurable, but I've never needed to change it.
Obviously, setting the history record to a longer interval will use up more disk space. It's not as much as you might think, though. PhpStorm, like most VCS tools, only saves the deltas from the previous version, so they're usually quite small. If you've got a large drive, you can actually set it to keep everything. Of course you could use Git for this, but using Local History is infinitely more convenient and you never have to leave the editor.
When you click on an entry in the Local History list, you see a side-by-side view of the changes between the file saved at that time and previous version. You can revert any of them with a click on the double arrow in the left panel. I often realize that I made a stupid change a few hours ago and want to revert but, but I can't remember the exact details of the change. As long as it's within the time window, reverting it becomes trivially easy. You just click on the double right arrow next to the change you want to revert.
In the course of writing this, I discovered a great feature of Local History that I didn't know about. You can right-click on elements of your code like classes, class variables, class methods, and code segments you select with your mouse and view the local history for just the element you've selected. You can also place labels on various changes you want to be able to find in the future. The labels are internal to PhpStorm and have nothing to do with Git or any other VCS.
In the next article, we'll look at how to compares files with each other in PhpStorm.
Looking for high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at A2 hosting. (More information in the box below.)