Fundamentals 8 min read

Recovering Wrongly Pushed Commits with Git in IntelliJ IDEA

This article explains how to revert accidental commits both locally and on the remote repository using simple compare‑versions techniques and IntelliJ IDEA's Git reset options, followed by a force‑push, while also warning about protected branches and noting the 2023 IDEA version specifics.

Top Architect
Top Architect
Top Architect
Recovering Wrongly Pushed Commits with Git in IntelliJ IDEA

In daily development we often use Git for version control, and sometimes push incorrect code to the remote repository or need to roll back to a previous commit for further development.

The author describes a simple, brute‑force method: compare the unwanted commit with the target commit, manually delete the erroneous code, and use the Compare Versions feature (Ctrl‑click two commits, then select Compare Versions) to remove the unwanted changes. This works well for small codebases but becomes cumbersome for complex configurations and leaves a record of the bad commit.

IDEA’s More Elegant Git Reset Method

1. Reset Current Branch to the Desired Commit

In IntelliJ IDEA, choose Reset Current Branch and select the hard option to restore the working tree to the chosen commit, discarding all local changes. The other options are:

Soft : keeps the working directory unchanged and preserves staged changes.

Mixed : keeps the working directory unchanged but unstages any staged files.

Hard : resets the working directory to the selected commit, losing all local modifications.

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

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

To synchronize the cleaned state to the remote, open the push dialog and perform a Force Push . This overwrites the remote history with the local state, effectively deleting the unwanted commits from the remote as well.

Note that force‑pushing is not allowed on protected branches; ensure the target branch is not protected before proceeding.

The author also mentions that the steps were performed with the 2023 version of IDEA, and if there are differences, using Git commands directly is an alternative.

Beyond the technical guide, the article contains promotional material for various services, including a ChatGPT subscription offering, a knowledge‑sharing community, and links to additional resources and open‑source projects.

gitIntelliJ IDEATutorialVersion 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.