Backend Development 8 min read

Reverting Mistaken Commits Locally and Remotely with Git and IDEA

This article explains step‑by‑step how to compare, delete, reset, and force‑push unwanted Git commits using both simple manual comparison and the more elegant reset options in IntelliJ IDEA, while also noting the limitations of complex changes and including promotional offers for related services.

Top Architect
Top Architect
Top Architect
Reverting Mistaken Commits Locally and Remotely with Git and IDEA

In daily development we often use Git for version control, and sometimes mistakenly push wrong code to a remote repository or need to roll back to a previous version for new development.

The author describes a scenario where an optimization was pushed without testing, and a new requirement forces the removal of that untested code.

Most Simple and Direct Method

If the erroneous code is not extensive, you can compare the current commit with the target commit (e.g., help documentation commit ) and manually delete the unwanted changes.

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

This works well for simple code but becomes cumbersome for complex configurations, and it leaves a record of the erroneous commit.

IDEA's More Elegant Git Method

1. Reset Current Branch to the Desired Commit

When resetting, IDEA presents four options; the author chooses hard . The other options are:

Soft : Keeps your working files unchanged and retains staged changes.

Mixed : Keeps your working files unchanged but unstages any staged files.

Hard : Restores files to the selected commit state, discarding all changes.

Keep : Restores files to the selected commit state while preserving local uncommitted changes.

After resetting, the erroneous commits disappear locally, but the remote repository still contains them.

To synchronize the state, you need to push the changes with Force Push . Note that this operation cannot be performed on protected branches.

After force‑pushing, the remote repository shows only the help documentation commit, with the three unwanted commits removed.

Note: The screenshots use IDEA 2023; if you encounter differences, consider using Git commands directly.

Beyond the technical guide, the article includes promotional content for a ChatGPT service, a knowledge‑sharing community, and various offers, encouraging readers to join and benefit from additional resources.

Backend DevelopmentgitIntelliJ IDEAVersion ControlResetforce push
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

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.