Fundamentals 6 min read

How to Undo a Pushed Commit in Git: Simple and IDEA‑Friendly Methods

This guide explains two practical ways to revert an already‑pushed Git commit—first by manually comparing versions and deleting unwanted changes, and second by using IntelliJ IDEA’s reset options followed by a force‑push to synchronize the remote repository.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How to Undo a Pushed Commit in Git: Simple and IDEA‑Friendly Methods

During a job interview, candidates may be asked how to revert code that has already been pushed to a remote Git repository. The article presents two solutions for developers who need to roll back to a previous commit, such as a help‑document update, while keeping the repository clean.

Simple brute‑force method

If the erroneous changes are limited, you can compare the current commit with the target commit, manually delete the unwanted code, and commit the cleaned version. In IntelliJ IDEA, hold Ctrl and select the two commits, then choose Compare Versions. After reviewing the diff, delete the undesired sections, commit the result, and push.

This approach works well for small code bases but becomes cumbersome with complex configurations, and it leaves the erroneous commit in the history, which may be undesirable for developers who prefer a clean log.

Elegant method using IDEA’s Git reset

1. Reset the current branch to the desired commit . Open the Git reset dialog in IDEA; you will see four options. Choose hard to discard all changes after the selected commit.

Soft:  your previous work stays unchanged, staged files remain staged.
Mixed: your previous work stays unchanged, staged files become unstaged.
Hard: files revert to the selected commit state; all local changes are lost.
keep: files revert to the selected commit state, but local uncommitted changes are kept.

After confirming the reset, the erroneous commit disappears locally, but the remote still contains it.

2. Force‑push the corrected history . Open the push dialog; even though there are no new commits, click the Force Push button to overwrite the remote branch. This operation is blocked on protected branches, so ensure the branch is not protected or adjust the repository settings.

The remote repository will now show only the intended commit (e.g., the help document) and the subsequent erroneous commits will be gone.

Note: The steps above were performed with the 2023 version of IntelliJ IDEA; earlier or later versions may have slight UI differences, and you can always achieve the same result with raw Git commands if preferred.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

software developmentIntelliJ IDEAForce PushUndo Commit
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

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.