Skip to content

Git Undo Push

DodaTech 3 min read

In this tutorial, you'll learn about Git Undo a Pushed Commit Fix. We cover key concepts, practical examples, and best practices.

You pushed a commit with a bug or wrong file, and need to undo it. There are two approaches: git revert (safe, creates a new commit) or git reset + force push (rewrites history). Choose based on whether others have pulled the branch.

The Problem

git push origin main

You realize the last commit contains a critical bug. Other developers may have already pulled it.

abc123 (HEAD -> main, origin/main) Fix critical bug — but it has a typo

Wrong Approach

# WRONG — resetting a shared branch
git reset --hard HEAD~1
git push --force origin main
# This breaks other developers' repositories

Right Approach

# SAFE — revert creates a new commit that undoes the changes
git revert HEAD

Expected output:

[main def456] Revert "Fix critical bug"
 1 file changed, 0 insertions(+), 5 deletions(-)
git push origin main

The history shows both commits, and the revert is safe for everyone.

Step-by-Step Fix

Step 1: Decide on the approach

# If others have pulled → use revert
# If only you worked on the branch → use reset

Step 2: Revert the commit

# Revert the last commit
git revert HEAD

# Revert a specific commit
git revert abc123

Step 3: Push the revert

git push origin main

Step 4: Alternative — reset and force push (solo branches only)

# Soft reset keeps changes staged
git reset --soft HEAD~1

# Hard reset discards changes (use carefully)
git reset --hard HEAD~1

git push --force-with-lease origin main

Step 5: Revert multiple commits

# Revert a range
git revert HEAD~3..HEAD

Step 6: Verify the result

git log --oneline -5

Prevention Tips

  • Push frequently in small commits to minimize revert scope
  • Use feature branches for experimentation
  • Review commits with git log and git diff before pushing
  • Use git revert for any branch that more than one person uses
  • Communicate before force pushing to shared branches

Common Mistakes with undo push

  1. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  2. Using return to exit a function early instead of wrapping a pure value in the monad
  3. Mixing let bindings with <- bindings in do notation, producing type errors

These mistakes appear frequently in real-world GIT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What is the difference between git revert and git reset?

git revert creates a new commit that undoes the changes, preserving history. git reset moves the branch pointer backward, removing commits from history. Always use revert for shared branches. Use reset only for unpublished local commits.

How do I undo a merge commit?

Use git revert -m 1 <merge-commit> to revert a merge. The -m 1 tells git to revert to the first parent (the branch before the merge). This preserves the merge history while undoing the changes.

Can I undo commits that are days or weeks old?

Yes. Use git revert <old-commit-hash> for any commit regardless of age. For reverts that conflict with intermediate changes, resolve conflicts like a normal merge. Git handles the undo by applying the inverse of the original commit's changes.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro