Undoing a 'git push'
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Undoing a pushed commit is not one universal command. The safe fix depends on whether anyone else may already be using that remote history. In general, use git revert on shared branches, and use history rewriting only when you control the branch and understand the consequences.
Safe Option: Revert the Pushed Commit
If the commit is already on a shared branch such as main, the safest way to undo it is to create a new commit that reverses the change.
This preserves history. Teammates do not have to repair their local branches because the bad commit remains in the log and the repository state is corrected by a new revert commit.
This is almost always the right answer for public or shared history.
Rewrite History Only on Branches You Control
If the pushed branch is your own feature branch and no one else depends on it, you can move the branch back and force-push the rewritten result.
--force-with-lease is safer than plain --force because it refuses to overwrite remote changes you have not seen yet.
This is appropriate when:
- you pushed a debug commit to your own branch
- you committed generated files by mistake
- you want to clean the branch before review
It is not the right default for shared branches.
If You Pushed to the Wrong Branch
If the commit belongs somewhere else, preserve the work first and then repair the mistaken branch.
After the work is preserved on the correct branch, fix the wrong branch with:
- '
git revertif it is shared' - reset plus force-push if it is private and safe to rewrite
That sequence reduces the chance of losing the work while you clean up the branch structure.
Undo Several Pushed Commits
For a private branch, you can reset to the commit that should become the new tip.
For a shared branch, create reverts instead.
Review the resulting diff carefully, especially if merges are involved.
Recovery Is Often Possible
If you make the cleanup worse locally, reflog is usually the first recovery tool to try.
That is why it is worth staying calm. Many local mistakes around resets and rewritten history are recoverable for a while.
A Simple Decision Rule
A practical rule of thumb is:
- shared branch: prefer
git revert - private branch:
git resetplusgit push --force-with-leasecan be acceptable - uncertain branch ownership: treat it as shared
That decision rule matters more than memorizing any single Git command.
Common Pitfalls
- Force-pushing rewritten history to a branch other people already use.
- Using plain
--forceinstead of--force-with-lease. - Running
git reset --hardbefore preserving work that still matters. - Treating
git revertandgit resetas interchangeable when one preserves history and the other rewrites it. - Forgetting
git reflogwhen a local recovery path still exists.
Summary
- Use
git revertto undo pushed changes on shared history safely. - Use reset plus
--force-with-leaseonly on branches you control. - Preserve work before repairing a push to the wrong branch.
- Use
git reflogif you need to recover from a local reset mistake. - Choose the undo method based on collaboration risk, not just command convenience.

