We all write a lot of code. Sometimes we are not sure of what the best version of the code will be, so we try different tweaks. Some of these tweaks might be great. But in some cases, we want to revert back to one we tried a while ago. How can we do that? How can we manage the history of our code and identify the best version to revert to? Read on to see how we can use IntelliJ code history for managing our code history.
First of all, it’s worth mentioning that when it comes to code history, in IntelliJ we have 2 options: history from the Version Control System (like Git), or the local history. You can think of the first one as a history of commits, whereas the second one is local to your machine and does not require code to be committed in order to find the changes within it. Let’s take a deeper look at both, and see how we can find the history for the bits of code we are interested in.
💡 Cool update: TestProject‘s free test automation platform just launched its 2.0 version, that now supports saving tests locally for complete offline executions and easy version management as part of a versioning control system such as Git.
Each Version Control System (VCS) we are using, like Git or SVN, has its own history mechanism. Therefore, if we browse the history of these repositories, using tools we normally connect to these repositories with, we will see what changes have been committed to these repositories. The history can include: files being created, files being deleted, files being changed or moved. The key thing here is that these changes need to be committed to the repository, in order to have access to them.
History of folders
When we are in the process of writing the code, we want easy access to the history of the code, without having to open additional programs for this task. Therefore, we can use IntelliJ to identify what we need. In the following paragraphs, I will refer to working with Git. In order to see the history of a folder or file, we can use the Show History option. That is because IntelliJ has integrated plugins for working with the VCS of your choice.
On a folder level, to access this history we need to right-click the name of the folder we are interested in. From the options that pop up, we will pick ‘Git’, then ‘Show History’ from the new menu that appears.
Because we are on the folder level, in the ‘Git’ pane (normally found on the lower part of the screen) we will see all the commits that refer to all the files inside this folder. We can filter the commits we see here. We can pick what branch to look in, what user made the changes, or we can filter based on the date of the commits.
If we click any of the commits, on the right-hand side, in the same pane, we will see what files this commit refers to. We will see which files were changed, created, or deleted. We can double-click any of the files, which will open the file view. Here we can look at what the changes are, or even revert to the selected version of the file. Each of the changes shown in this history refers to something that was changed from the previous version of the file. For example, for commit N, a change means a difference between the same code from commit N-1 and the same code in commit N.
History of files
In case we are not interested in the changes for an entire folder, we can simply just pick a file in the navigation. We can then right-click the file name, and again choose ‘Git’ and ‘Show History’. This time, in the Git pane, we will get the list of commits that affect only the selected file. We can browse through them to see the changes made to the entire file.
History of bits of code
In some cases, we are only interested in the history of a small chunk of code. For such a task, choosing the file, and viewing the entire file history might not be the best option. That is because you would need to actually find the code you are interested in, inside the file. That means an extra step, which is not always fast or easy.
To only visualize the history of a certain piece of code, from the editor we need to select it, then right-click, choose ‘Git’, and then choose ‘Show History for Selection’. In the new window that appears, you can browse through the list of commits affecting only this code. You will also get a nice side-by-side comparison between the code changed in the selected revision and the previous version for the same code. This is much easier to read and more relevant than the previous options.
In some cases, we might have some obsolete files in our project. These might be test classes that were used at some point, but for whatever reason are not needed anymore. Keeping obsolete files in the project is not a good idea, as it can cause problems. Just to name one: too many files will make syncing with the repository very slow at one point. Therefore, obsolete files should be deleted.
Many of us would be reluctant to remove these unused files, thinking that maybe at some point we will need them again. But there is no need to keep them, as we can recover them, when needed, from the VCS history. We just need to browse the commit list to find when the files were deleted, and we can recover them from that commit. Nothing gets lost due to the commit history.
When we are writing code with IntelliJ, we have access to an additional type of history: the local one. This mechanism allows us to browse the changes we made on our machine, without the need to have all the changes committed to the repository. You can think of this feature as a volatile history: it only keeps by default changes of 5 days of work. And it basically is an auto-save of your changes at different stages of your work.
History of folders
Similar to the VCS history, you can get the local history for an entire folder. Just right click the name of the folder in the navigation, then choose ‘Local History’ and ‘Show History’. This will bring up a list of checkpoints on the left-hand side. They all have a timestamp, which can guide you in finding the checkpoint you are interested in. Clicking on each checkpoint you can see what files have changed at that checkpoint. The files are listed on the right-hand side.
Double-clicking any of the listed files, you can see what the changes are. They are shown as a side-by-side view of the same file.
History of files
If we are only interested in the local history of a file, while in the navigation pane, we should right-click the file name. Then, choose ‘Local History’ and ‘Show history’. This will again bring up a list of checkpoints. This time, clicking on a checkpoint will directly show, in the same window, the side-by-side view of the current (for that checkpoint) and the previous version of the file. All changes are highlighted in this case.
History of bits of code
When you are only looking for the history of a smaller chunk of code, the previous approaches will make it more difficult to find what you are looking for. Therefore, in order to focus only on the needed code, first you need to select it. Then, right-click on it, choose ‘Local History’, and this time choose ‘Show History for Selection’. This will again bring up the checkpoint list. But this time, in the side-by-side view, you will only see the selected code, in its’ previous and current (for that checkpoint) version.
It is easy to revert to or simply visualize a previous change for our code using the IntelliJ code history options. If the change we are interested in is already in our Version Control repository, we can look into the commit history. However if the changes did not make it to the repository yet, Local History comes in handy.