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