How to Undo a Pushed Git Commit? Interviewers Often Stump Candidates
The article walks through four practical ways to roll back code that has already been pushed to a remote Git repository—manual copy‑and‑compare, using git revert, creating a new branch from the desired commit, and resetting the current branch—while highlighting the trade‑offs and safety considerations of each method.
In everyday development it is easy to push erroneous code to a remote repository or need to revert to an earlier version for a new requirement; the author wants both the local and remote branches restored to the "help documentation" commit.
1. Manual copy and compare (not recommended)
This approach involves selecting the two commits to compare in the IDE, using the "Compare Versions" feature, and manually deleting the unwanted code. It works for simple changes but becomes cumbersome with complex code or many configuration files.
2. git revert (recommended for single commits)
Right‑click the erroneous commit in the IDE and choose "Revert". Git automatically creates a revert commit that undoes the changes and preserves history. After the revert commit is created, push it to the remote repository to complete the rollback. The method is safe because it leaves a clear record, but it can only revert one push at a time, making multiple reverts time‑consuming.
3. Create a new branch from the target commit (recommended for many commits)
Right‑click the desired commit and select "New Branch". This creates a branch that points directly to the good commit, preserving the original history while allowing work to continue from the correct state. After switching to the new branch, force‑push it to overwrite the remote history. The author warns that proliferating branches can make branch management difficult.
4. Reset the current branch to the target commit (use with caution)
Use the IDE’s reset dialog and choose one of the four options:
Soft : keeps working directory changes and staged files.
Mixed : keeps working directory changes but unstages files.
Hard : discards all changes and makes the working directory match the selected commit.
Keep : discards the commit but retains uncommitted local changes.
After a hard reset, force‑push the branch to update the remote repository. This operation is unsafe for protected branches and should be used only when you understand the consequences.
Summary recommendation
For a single erroneous push, git revert is the safest choice. When many commits need to be undone, creating a new branch from the good commit is more efficient. Resetting with hard should be a last resort and only on unprotected branches. The screenshots illustrate the steps in the 2023 version of IntelliJ IDEA; the same actions can be performed with Git commands if the UI differs.
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 Architect Handbook
Focused on Java interview questions and practical article sharing, covering algorithms, databases, Spring Boot, microservices, high concurrency, JVM, Docker containers, and ELK-related knowledge. Looking forward to progressing together with you.
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.
