In our daily development, sometimes we may realize that the current state of our code is not ideal while we are writing it. In such situations, we may want to revert the changes of the code that have not been committed to the latest commit version. So, how can we achieve this?
That’s right, we can manually revert the code changes one by one. However, instead of doing it manually, we can use the “Rollback” feature to revert all the changes at once. Below is the demonstration of how to use the Rollback feature for reverting the code to the previous commit state.
Before performing the IntelliJ IDEA Rollback operation, there are some prerequisites to consider.
Before performing the Rollback operation, for the sake of demonstration, let’s make some simple modifications to our code. It’s best to include three types of changes: code modification, new files (unstaged), and new files (staged). This will help us better understand the impact of the Rollback operation on the code status. The modified code files will look like the following, containing three types of changes.

(Quick Tip: If you are not familiar with staging files, you can refer to the first section of EP5 in this tutorial series – How to Perform Local Project Repository Commit – COMMIT) (Link)
IntelliJ IDEA Rollback Operation Demo
Rollback Operation for Modified but Uncommitted Files (in Blue)
1.Original File Content before the Operation

2.To perform the Rollback operation, right-click on the file, then select “Git” -> “Rollback” from the context menu.

3.A window titled “Rollback Changes” will appear, where you can see:
- The selected file for the Rollback operation.
- Information about the affected file states and their quantities in this operation.
Simply click the “Rollback” button to proceed.

4.Now you can see that the file has turned back to white, and its content has been reverted to its original state.

For the added and indexed files (green), the Rollback operation is as follows
1.The content of the file before the operation is as follows.

2.Perform the Rollback operation by right-clicking on the file and selecting “Git → Rollback” in the context menu.
3.At this point, a window titled “Rollback Changes” will appear, and we can simply click the “Rollback” button.
4.At this point, the file’s color will change to red (indicating an unstaged state) and the content of the file remains unchanged.

5.So, we can observe that when performing the Rollback operation on a newly added and already indexed file, it only reverts the file to the unstaged state. Is there a way to automatically delete the file during the Rollback operation? The answer is yes.
Rollback operation on the modified but uncommitted file (green) – Advanced Version
1.Let’s first stage the file again.

2.Perform the Rollback operation again. Right-click on the file, then choose “Git -> Rollback”.
3.In the popped-up window “Rollback Changes,” we can find an option “Delete local copies of added files” at the bottom. We checkmark this option, which means that we want to delete the files that were added to the index while performing the Rollback operation. Finally, click the “Rollback” button.

4.At this point, we can see that the files have also been deleted. Isn’t it much more convenient?
To operate on newly added but untracked files (in red color)
When performing a Rollback operation on such untracked files (in red color), you may notice that the Rollback button is disabled. This is because Rollback cannot be applied to files that have not been added to the index. In most practical scenarios, encountering such situations is rare. If you come across this situation, you can simply delete the file directly.

Summarize
To summarize, in this tutorial, we learned about the Rollback operation in IntelliJ IDEA. We explored how to rollback changes for modified files (in blue color), revert added but uncommitted files (in green color), and handle untracked files (in red color). By using Rollback, we can efficiently revert our code to the most recent committed state or discard changes as needed.
So, now that we’ve learned Rollback operations, doesn’t it make it more efficient for us to start over when needed? Finally, Booster has prepared the following table to help you understand more easily and quickly how Rollback operations affect files in different states.
| File Status | Rollback Operation | Result |
|---|---|---|
| Modified (Blue) | Right-click -> Rollback | Reverts the file to the last committed state |
| Added (Green) | Right-click -> Rollback -> Check “Delete local copies of added files” | Removes the file from both the index and the local copy |
| Untracked (Red) | Delete the file manually | Since Rollback is not applicable to untracked files, manually delete them |
With this table, you can confidently handle various file scenarios using Rollback and enhance your development workflow effectively.




