git
version control
git push
git commands
troubleshooting

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.

bash
git log --oneline
git revert abc1234
git push origin main

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.

bash
git reset --hard HEAD~1
git push --force-with-lease origin my-feature

--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.

bash
1git switch wrong-branch
2git branch rescue-work
3git switch correct-branch
4git cherry-pick rescue-work

After the work is preserved on the correct branch, fix the wrong branch with:

  • 'git revert if 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.

bash
git log --oneline
git reset --hard 4f3c2b1
git push --force-with-lease origin my-feature

For a shared branch, create reverts instead.

bash
git revert HEAD~2..HEAD
git push origin main

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.

bash
git reflog
git reset --hard HEAD@{1}

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 reset plus git push --force-with-lease can 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 --force instead of --force-with-lease.
  • Running git reset --hard before preserving work that still matters.
  • Treating git revert and git reset as interchangeable when one preserves history and the other rewrites it.
  • Forgetting git reflog when a local recovery path still exists.

Summary

  • Use git revert to undo pushed changes on shared history safely.
  • Use reset plus --force-with-lease only on branches you control.
  • Preserve work before repairing a push to the wrong branch.
  • Use git reflog if you need to recover from a local reset mistake.
  • Choose the undo method based on collaboration risk, not just command convenience.

Course illustration
Course illustration

All Rights Reserved.