How to Undo a Pushed Commit in Git? Interview‑Level Solutions
The article walks through four practical ways to revert code that has already been pushed to a remote Git repository—including manual diff deletion, using git revert, creating a new branch, and resetting with hard/soft options—while highlighting each method's steps, trade‑offs, and when to use force‑push.
During an interview the candidate is asked how to roll back code that has already been pushed to a remote Git repository. The author describes a typical situation where an accidental or premature push needs to be undone without losing the rest of the project history.
1. Manual compare‑and‑delete (basic, not recommended)
If the erroneous changes are few, you can compare the current commit with the target commit (e.g., the "help documentation" commit) using the IDE’s Compare Versions feature, manually delete the unwanted code, and commit the cleaned version. This works for simple code but becomes cumbersome for complex changes or large configuration files, requiring repeated comparisons.
2. git revert (recommended for single commits)
Right‑click the erroneous commit in the Git history and select Revert . Git automatically creates a new revert commit that undoes the changes of the selected commit. Push the revert commit to the remote repository; the history now contains a safe, explicit rollback. This method is safe and leaves a clear record, but it can only revert one commit at a time, making it inefficient for dozens of bad commits.
The revert commit is created automatically ( revert) and can be pushed like any other commit.
3. Create a new branch (good for large rollbacks)
When you need to go back many commits, right‑click the desired commit and choose New Branch . This creates a branch that points to the target commit while preserving the original branch. After checking out the new branch, push it with Force Push to overwrite the remote history. The original commits remain in the old branch, providing a safety net, but excessive branching can complicate branch management.
4. Reset current branch to a specific commit (forceful, risky)
Use Reset on the current branch and select one of the four modes:
Soft : moves HEAD to the chosen commit but leaves index and working tree unchanged.
Mixed : moves HEAD and updates the index, leaving the working tree unchanged.
Hard : resets HEAD, index, and working tree to the chosen commit, discarding all local changes.
Keep : resets HEAD and index while preserving uncommitted working‑tree changes.
After a hard reset, push the branch with Force Push to rewrite the remote history. This operation cannot be performed on protected branches.
Finally, verify that the remote repository now ends at the "help documentation" commit, with the intervening erroneous commits removed.
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.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
