Fundamentals 6 min read

How to Revert Local and Remote Git Commits Using IntelliJ IDEA

This guide explains how to undo unwanted commits both locally and on a remote Git repository using IntelliJ IDEA, covering simple manual comparison, the IDE's reset options, and force‑push to synchronize the cleaned history.

Java Captain
Java Captain
Java Captain
How to Revert Local and Remote Git Commits Using IntelliJ IDEA

In daily development we often use Git for version control, but sometimes we accidentally push wrong code to a remote repository or need to roll back locally to a previous version for further work.

For example, after pushing an optimization that seemed useful later, a new requirement arrived; because the project is critical, untested changes cannot be deployed, so the pushed optimization must be removed first.

My branch situation looks like this, and I want to restore both the local and remote repositories to the state of the "help document" commit.

最简单粗暴的方法

If the erroneous code is small, you can compare it with the commit you want to revert to, manually delete the wrong code, and remove the differences.

Hold Ctrl and select the two commits you want to compare, then choose Compare Versions to delete the code you wish to remove.

This approach works well for simple code and can even ensure consistency by comparing the latest commit after deletion with the target commit.

However, it becomes cumbersome for complex codebases, especially when dealing with intricate configuration files.

Moreover, it leaves a record of the erroneous commit, which can be unsatisfactory for perfectionists; Git also offers a more elegant workflow to solve this.

IDEA 中 Git 较为优雅的方法

1. Reset Current Branch 到你想要恢复的commit记录

When you choose Reset, four options appear; I selected hard .

The other options are provided for reference only, as I haven't tried each of them.

Soft : Your previous work remains unchanged and any staged files stay staged.

Mixed : Your previous work remains unchanged but staged files become unstaged.

Hard : Files are restored to the selected commit state; all changes are lost, including any local modifications made after the commit.

keep : Local changes are kept while the files are restored to the selected commit state; uncommitted local edits remain.

After the reset, the erroneous commit disappears locally, but the remote repository still contains the original commits, so you need to push the current state to the remote, effectively deleting those commits there as well.

Open the Push dialog; even though there are no new commits to submit, you must click Force Push to push the changes.

Note that this operation cannot be performed on protected branches; you need to check the branch protection settings. In my case, I was not on the master branch, so there was no protection.

You can see that the latest commit in the remote repository is now only our "help document"; the three commits above it have disappeared.

Note: The above steps use the 2023 version of IntelliJ IDEA; if you encounter differences, consider using the corresponding Git command‑line operations.

gitIntelliJ IDEAVersion Controlbranch managementresetforce push
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.