Fundamentals 7 min read

How to Undo a Pushed Commit in Git: Revert, Reset, and Branch Strategies

Learn step-by-step how to undo mistakenly pushed commits in Git, covering manual diff deletion, the recommended 'git revert' command, creating a new branch for large rollbacks, using 'git reset' with soft/mixed/hard/keep options, and safely force‑pushing the corrected history.

Su San Talks Tech
Su San Talks Tech
Su San Talks Tech
How to Undo a Pushed Commit in Git: Revert, Reset, and Branch Strategies

1. Manual comparison and deletion (not recommended)

This approach involves comparing the erroneous commit with the target commit, manually selecting the code to delete, and using the IDE's "Compare Versions" feature to remove unwanted changes. It works for simple code but becomes cumbersome with complex configurations.

2. git revert (recommended)

Right‑click the erroneous commit in the history and select "Revert Commit". Git automatically creates a revert commit that undoes the changes introduced by the bad commit while preserving history.

After the revert commit is created, push it to the remote repository to complete the rollback. This method is safe because it records the revert operation, but it can be inefficient when many commits need to be undone, as each revert creates a separate record.

3. Create a new branch (useful for large rollbacks)

If you need to revert dozens or hundreds of commits, create a new branch at the desired commit point. Right‑click the target commit and choose "New Branch".

This adds a branch that preserves the original history while allowing you to work from the older commit safely. Use it sparingly, as excessive branching can complicate branch management.

4. Reset the current branch to a specific commit (use with caution)

Use git reset with one of the following options:

Soft : Keeps the working directory unchanged; staged changes remain.

Mixed : Resets the index but leaves the working directory untouched.

Hard : Resets both index and working directory to the selected commit, discarding all local changes.

Keep : Resets the index to the commit while preserving uncommitted local changes.

After resetting, the erroneous commits disappear locally, but the remote still contains them. To synchronize, force‑push the corrected state.

Open the push dialog, select Force Push , and push the changes. Note that force‑push is prohibited on protected branches.

After the force push, the remote repository shows only the desired help documentation commit; the three unwanted commits are gone.

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.

GitVersion ControlBranchresetrevertForce Push
Su San Talks Tech
Written by

Su San Talks Tech

Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.

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.