Fundamentals 7 min read

How to Revert Commits and Reset Branches in Git Using IntelliJ IDEA

This article explains several methods for undoing unwanted Git commits—including a simple compare‑and‑delete approach and a more elegant reset‑and‑force‑push workflow in IntelliJ IDEA—covering both local and remote repository restoration while highlighting the differences between soft, mixed, hard, and keep reset options.

Architecture Digest
Architecture Digest
Architecture Digest
How to Revert Commits and Reset Branches in Git Using IntelliJ IDEA

Source: cnblogs.com/fnz0/p/15803011.html. In the backend, click the menu “Learning Materials” → “Books” and reply “5000” to receive the free "Programmer Book Resources".

In daily development we often use Git for version control. Sometimes we accidentally push wrong code to the remote repository, or we want to roll back locally to a previous version for further development.

Or, like me, I pushed some optimization proposals that I thought would be useful later, and then a new requirement arrived. Because the project is important, untested proposals cannot be deployed lightly, so to handle the new requirement I have to remove the pushed optimization first.

Now my branch looks like this, and I want to restore both the local and remote repositories to the part of the "help document" commit.

The simplest and most brute‑force method

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

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

This method works well when the code is simple, and you can even ensure consistency by comparing the latest commit after deletion with the commit you want to revert to.

However, this method is not suitable for complex code, especially when dealing with complicated configuration files.

Moreover, this leaves the erroneous commit record, which a perfectionist may find unacceptable. Git also provides a more elegant workflow that can solve this problem.

A more elegant method in IDEA

1. Reset Current Branch to the commit you want to restore

At this point four options appear; I chose hard .

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

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

Mixed : Your previous work remains unchanged, but previously staged files are unstaged.

Hard : Files are restored to the selected commit state; any changes are lost. If you have committed and then made local changes, selecting hard will discard both the committed content and the uncommitted local changes.

Keep : Any local changes are kept, while the files are restored to the selected commit state. If you have committed and then made local changes, selecting keep will discard the committed content but keep the uncommitted local changes.

Then the erroneous commit is removed locally, but the remote repository still has the original commits; you need to push the current state to the remote, i.e., push the deletion of those commits.

Open the push dialog; although there is nothing to commit, you need to click Force Push to force‑push the changes.

Note that for protected branches this operation is not allowed. Check the branch protection settings; in my case the branch is not protected.

You can see that the latest commit in the remote repository is only our help document . The three commits above are gone.

Note: The above steps were performed with IDEA 2023 version; if there are differences, consider using Git commands directly.

END

gitIntelliJ IDEAVersion Controlresetforce pushCommit Revert
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

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.