How to Roll Back Pushed Git Commits: Practical Methods
The article explains four ways to undo code that has already been pushed to a remote Git repository—including manual diff deletion, using git revert, creating a new branch, and resetting the current branch—detailing each method’s steps, trade‑offs, and safety considerations.
Interviewers often ask how to revert code that has already been pushed to a remote Git repository, and many developers are unsure of the best approach.
1. Basic manual operation (clumsy, not recommended)
2. git revert (recommended)
3. Create a new branch (recommended for large rollbacks)
4. Reset current branch to a specific commit (unsafe, use with caution)
1. Basic Manual Operation (Clumsy, Not Recommended)
If the erroneous code is small, you can compare the current commit with the target commit, manually delete the unwanted changes, and use the IDE’s “Compare Versions” feature to ensure consistency. This works for simple code but becomes cumbersome with complex configurations.
2. git revert (Recommended)
When a wrong commit has already been pushed, right‑click the erroneous commit in the IDE and select “Revert”. Git automatically creates a revert commit that undoes the changes, preserving history. Push the revert commit to the remote to complete the rollback.
The revert operation is safe because it records the rollback, but it can only undo one push at a time; reverting many commits requires repeated actions, which can be tedious.
3. Create a New Branch (Recommended for Large Rollbacks)
If you need to revert to a state many commits ago, create a new branch from the desired commit: right‑click the target commit and choose “New Branch”. This preserves the original history while giving you a clean branch to work from, avoiding a flood of revert records.
While this method keeps both the original and the rolled‑back versions, excessive branch creation can complicate branch management.
4. Reset Current Branch to a Specific Commit (Unsafe, Use Cautiously)
Select the target commit and choose the “hard” reset option. The reset dialog offers four strategies:
Soft : Keeps your working directory unchanged; staged changes remain.
Mixed : Keeps the working directory but unstages changes.
Hard : Discards all changes and makes the working tree exactly match the selected commit.
Keep : Discards the commit but preserves uncommitted local changes.
After resetting, the erroneous commit disappears locally, but the remote still contains it. To synchronize, open the push dialog and perform a “Force Push”. This operation is blocked on protected branches.
After the force push, the remote history shows only the desired commit (e.g., the “help documentation” commit), and the three unwanted commits are removed.
Note: The screenshots were taken with the 2023 version of IntelliJ IDEA; command‑line alternatives may be needed for other environments.
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.
Programmer XiaoFu
xiaofucode.com – a programmer learning guide driven by the pursuit of profit
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.
