How to Undo a Pushed Commit in Git: Revert, Reset, and Branch Strategies
This guide explains multiple ways to roll back code that has already been pushed to a remote Git repository, covering manual comparison, the git revert command, creating a new branch, and using reset with force‑push while highlighting safety considerations.
1. Manual comparison (not recommended)
If you are unfamiliar with Git commands, you can use your IDE’s Compare Versions feature to compare the erroneous commit with the target commit, manually delete the unwanted changes, and commit the corrected state. This method works only for very small changes and becomes impractical for complex code bases.
2. git revert (recommended)
Use git revert to create a new commit that undoes the changes introduced by a specific bad commit while preserving history. git revert <bad_commit_hash> After the revert commit is created, push it to the remote repository: git push origin <branch_name> The revert commit records the rollback, making the operation safe and auditable.
3. Create a new branch from the desired commit (useful for large rollbacks)
When you need to return to a version that is many commits behind, create a new branch at that commit and continue development from there.
git checkout -b rollback-branch <target_commit_hash>This preserves the original history while giving you a clean line of development. Remember that excessive branching can complicate branch management.
4. Reset the current branch to a specific commit (use with caution)
Git provides four reset modes. Choose the one that matches the desired outcome:
Soft : Keeps the working directory unchanged; staged changes remain. git reset --soft <commit_hash> Mixed (default): Resets the index but leaves working files untouched. git reset --mixed <commit_hash> Hard : Discards all changes and makes the working directory match the selected commit. git reset --hard <commit_hash> Keep : Resets to the selected commit but preserves uncommitted local changes. git reset --keep <commit_hash> After a hard reset, the local history no longer contains the erroneous commits, but the remote repository still does. To synchronize the remote branch, force‑push the new state: git push --force origin <branch_name> Force‑push is prohibited on protected branches; ensure the branch is not protected before using this option.
Author: DaveCui Source: juejin.cn/post/7307066452290043958
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.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
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.
