Beyond Comparison: Compare Anything in IntelliJ IDEA
IntelliJ IDEA provides powerful coding assistance features, smart code completion, tons of inspections and context actions, and much more. But IntelliJ IDEA is not just an editor. It has powerful features that can make your user experience pleasant and easy as well. Today, we’ll take a look at the options you have for comparing various elements in IntelliJ IDEA.
Comparing project files
Let’s start with comparing files.
Open the Project tool window by pressing ⌘ 1 on macOS (Alt+1 on Windows and Linux), or navigate to View | Tool Windows | Project. From there, select the files that you want to compare and press ⌘ D (Ctrl+D). If necessary, you can compare three files instead of just two.
IntelliJ IDEA will open a built-in diff viewer that allows you to clearly see all of the differences between the files and navigate between them.
Similarly, IntelliJ IDEA allows you to compare two folders. Just select them in the Project tool window and press ⌘ D (Ctrl+D).
Comparing project files with non-project files
IntelliJ IDEA allows you to compare project files with files stored outside of the project.
In the Project tool window, select the first file that you want to use in the comparison and press ⌘ D (Ctrl+D). In the next dialog, select the file that you want to use for comparison.
That’s it! IntelliJ IDEA will open the diff viewer for you and highlight the differences.
Comparing the editor with the clipboard
If you have a code fragment copied to the clipboard and you want to compare it with a file in your project, you don’t need to create a new file.
Just open the file you want to use for the comparison in the editor and invoke its context menu by right-clicking anywhere in the file. Select Compare with Clipboard, and the diff viewer will show you the differences in a separate tab.
Comparing revisions in the local history
The local history automatically records revisions to your project, triggered by various events as you edit code, run tests, deploy applications, and so on. It acts as your personal version control system and lets you restore deleted files, revert specific changes, and more.
IntelliJ IDEA provides a diff viewer that allows you to easily track changes recorded by the local history and roll back specific modifications using the button in the gutter.
For example, right-click a file in the Project tool window and select Local History | Show History. The diff viewer opens, allowing you to see the differences between the file’s revisions.
Comparing with a branch
If your projects are under version control, you might find this feature especially useful. It allows you to compare a file in your current branch with the same file in another branch. Sometimes this is much faster and more convenient than browsing the file’s history.
In the Project tool window – ⌘ 1 (Alt+1) or View | Tool Windows | Project – right-click a file and select <Name of your version control system> | Compare with Branch. For example, if you use Git as your VCS, your path will be Git | Compare with Branch.
From there, select the branch that you want to use for comparison. The diff viewer will open, and you will be able to easily transfer the required changes to your current branch by using the button in the gutter.
Comparing modified lines right in the editor (Git)
This feature is slightly different from the previous ones, but it still provides a diff viewer that offers a convenient way to review changes to a file.
Suppose you’re investigating changes to a file and you open Git blame annotations to identify the last person who modified a line and see what the modification was. Right-click the gutter and select Annotate with Git Blame.
All you need to do is to hover the mouse pointer over an annotation, and IntelliJ IDEA will show you the modifications right in the editor.
On top of that, you can open a blank diff viewer and compare any sort of text. Press ⇧⌘A (Ctrl+Shift+A) to open Find Action and type “Open Blank Diff Window”. In the tab that opens, paste two versions of any piece of code or text to see how they differ.
You can try all of these features in the latest version of IntelliJ IDEA 2022.1, which you can download from our website.
Subscribe to Blog updates
The New Terminal (Beta) Is Now in JetBrains IDEs
The new terminal (Beta) is now in JetBrains IDEs and will be available in the new UI starting from the 2024.1 version of the IDEs.
‘HelloWorld’ and ‘main()’ meet minimalistic
"Just ignore the terms class, public, static and arg for now; we'll talk about it later". If you have ever mentioned these lines to a new Java student, who is about to execute their first 'HelloWorld' program, know that this is changing. If you are that student, well, congratulations, getting starte…
Easy Hacks: How To Implement Polymorphism in Java
Polymorphism is the ability of an object to take on different forms. In programming, this means that a variable or a method can have different behaviors depending on the type of object it represents. While the term may sound intimidating to beginners, polymorphism is a powerful tool: it helps you re…
Constructor Makeover in Java 22
Typically, we create alternative solutions for tasks that are necessary, but not officially permitted. For instance, executing statements before super() in a derived class constructor was not officially allowed, even though it was important for, say, validating values being passed to the base class …