How to Undo a Pushed Git Commit: Revert, Reset, and Branch Strategies
When a mistaken commit has already been pushed, this guide explains four practical ways to roll back the changes—manual comparison, git revert, creating a new branch, and resetting the current branch—detailing each method's steps, risks, and when to use them.
During development, developers sometimes push erroneous code to a remote repository and need to restore a previous state. This article presents four approaches to revert a pushed commit, explaining the workflow, advantages, and precautions for each.
1. Basic Manual Operation (not recommended)
This method involves manually comparing the unwanted commit with the target commit using the IDE's Compare Versions feature, deleting the undesired code, and committing the cleaned version. It works for simple codebases but becomes cumbersome for complex changes or many files.
2. git revert Commit (recommended)
When the erroneous commit has already been pushed, right‑click the offending commit in the IDE and select Revert Commit . Git automatically creates a new revert commit that undoes the changes, preserving history. After the revert commit is created, push the changes to the remote repository (often using Force Push if the branch is protected).
The revert commit safely records the rollback, making it a preferred choice for most cases. However, each revert creates a separate commit, which can be tedious if dozens of commits need to be undone.
3. Create a New Branch (useful for large rollbacks)
If many commits must be discarded, create a new branch from the commit you want to keep. Right‑click the desired commit, choose New Branch , and then push the new branch to the remote repository. This preserves the original history while allowing you to work from a clean state.
While this method avoids a long list of revert commits, it adds a new branch that must be managed, so use it judiciously.
4. Reset Current Branch to a Specific Commit (unsafe, use with caution)
Resetting rewrites history by moving the branch pointer to a chosen commit. The IDE offers four reset modes:
Soft : Keeps working directory and index unchanged; only the commit pointer moves.
Mixed : Resets the index but leaves the working directory untouched.
Hard : Discards all local changes and makes the working directory match the selected commit. Any uncommitted work is lost.
Keep : Resets to the selected commit but preserves uncommitted local changes.
After a hard reset, you must force‑push the branch to update the remote repository, which may be blocked on protected branches.
Finally, push the reset state with Force Push to overwrite the remote history.
Note that protected branches may reject a force push; ensure the branch is not protected before proceeding.
After the operation, the remote repository will only contain the desired commit (e.g., the help documentation), and the erroneous commits will be removed.
Note: The screenshots were taken with IntelliJ IDEA 2023; command‑line equivalents are also available.
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.
