Reverting Mistaken Commits Locally and Remotely with IntelliJ IDEA
This guide explains how to revert mistakenly pushed code by either manually comparing and deleting unwanted changes or using IntelliJ IDEA's Git reset options (soft, mixed, hard, keep) followed by a force push, ensuring both local and remote repositories return to a specific commit.
1. Simple Direct Method
If the erroneous code changes are few, you can compare the current state with the target commit, manually delete the unwanted lines, and then remove the differing files. In IDEA, hold Ctrl and select the two commits to compare, then choose Compare Versions to see differences and delete the undesired code.
This approach works well for simple codebases but becomes cumbersome for complex projects with many configuration files, and it leaves a record of the erroneous commit in the history.
2. Elegant Method Using IDEA’s Git Integration
Reset Current Branch to the Desired Commit
Open the Git log in IntelliJ IDEA, locate the commit you want to revert to (e.g., the "help documentation" commit), right‑click it, and select Reset Current Branch to Here . IDEA presents four reset options:
Soft : Keeps your working directory unchanged; staged changes remain.
Mixed : Resets the index but leaves the working files untouched.
Hard : Restores files to the selected commit state, discarding all local changes.
Keep : Resets to the commit while preserving uncommitted local modifications.
Choose the option that matches your situation (the author selected hard to discard all local changes).
After the reset, the erroneous commits disappear locally, but the remote repository still contains them. To synchronize the remote repository, open the push dialog. Even if there are no new commits to push, click Force Push to overwrite the remote history with your local state.
Note: Force‑push is prohibited on protected branches (e.g., master) unless the protection is disabled. Ensure the branch you are updating is not protected.
After the force push, the remote repository shows only the desired commit (the "help documentation" commit), and the three unwanted commits are gone.
Important: The screenshots in the original article illustrate the IDEA UI steps; they have been retained for reference.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
