Compare files and Git changesets in PhpStorm
Learn how to compare file versions and inspect changesets using integrated version control tools.
Lesson Content
PhpStorm has built-in Git tools that make it easy to review changes, compare files, and identify who changed specific lines.
Start with a Magento module declaration in etc/module.xml. Remove the sequence nodes so the module uses a self-closing module tag, then add a few blank lines at the end of the file.
As soon as you change the file, PhpStorm displays colored bars in the editor gutter. These bars identify lines that differ from the last committed version. Click a bar to open the available actions for that changeset.
The Rollback Lines action restores the selected lines to their previous state. Use Show Diff to open the diff viewer and inspect exactly what changed. The Copy action copies the version of the file from before your local changes, which is useful when you want to compare the previous content directly within the editor.
You can also right-click in the gutter and select Annotate with Git Blame. Git annotate shows which commit and author last changed each line. This is especially helpful when you work on a larger team and need to find out who introduced a particular change. Click an annotation to open the commit that contains the change.
PhpStorm also lets you compare a file with any Git revision from the Project tool window. Right-click a file, open the Git menu, and select Compare with Revision. Choose the revision you want to compare against to open the diff viewer.
The diff viewer provides several options for controlling how changes appear. The default Side-by-Side Viewer shows the previous version and your local version in separate panes. Change it to Unified Viewer to display the changes inline, then switch back to Side-by-Side Viewer when you want to see both versions next to each other.
You can also control how PhpStorm displays whitespace changes. The Do not ignore option shows every difference, including whitespace. Select Ignore white spaces and empty lines when formatting changes make it harder to identify meaningful code changes.
In the side-by-side view, the left pane contains the previous commit and the right pane contains your local working changes. You can edit the file directly in the diff viewer, which is useful when you want to clean up several changes while reviewing them.
The diff viewer also includes an arrow for accepting a change from the previous version. Click it to bring that change back into your local file.
PhpStorm can compare files that are not necessarily different Git revisions. For example, open a custom Config class that extends Magento\Sales\Model\Config. When you review a class like this, you may want to see exactly what it adds or changes compared with its parent class.
Open the parent class, select and copy its contents, and then return to your custom class. Right-click inside the editor and select Compare with Clipboard. PhpStorm opens a diff showing the clipboard contents alongside the current file.
Scroll through the comparison to see where the custom class differs from the core class. If the custom class only adds a single function, this comparison can reveal duplicate functions inherited from the parent and help you refactor the class to keep only the code that actually needs to be customized.
Join the discussion!
Comments
Want to comment on this lesson?
Log in to leave comments.