...

CSNAINC

how to undo pushed commits with git blog banner

Undo a Git Push – How to Revert, Reset, or Recover Commits

You pushed something you shouldn’t have. Or it was an unfinished feature. Maybe a test line you meant to delete. Maybe even a .env file with your API key. We’ve all been there. The good news is that Git makes it pretty easy to fix. You just need to know which method fits your situation.

In this guide, we’ll walk through 4 different ways to undo a git push, when to use them, and what each one actually does behind the scenes.

When Should You Revert vs Reset?

flowchart showing when to use git revert vs git reset

This is the first decision you need to make.

Ask yourself:
Is this commit already live on a shared branch? Like main, or anything your teammates are working on

  • If yes → use git revert. It’s the safest way to undo a commit without messing up shared history.
  • If no → you’re fine to use git reset. Just be careful with force pushing afterward.

How to Undo a Git Push Using git revert

Let’s say you pushed something bad to main. Maybe you want to undo it, but you don’t want to mess with anyone else’s work. That’s where git revert comes in.

It doesn’t delete the commit. Instead, it creates a new one that reverses it.

Step-by-step:

  git log --oneline      # grab the commit hash
git revert abc123      # change abc123 to your actual commit
git push
  

You’ll be asked to confirm a commit message. You can just save and close the file, or write your own.
Use this when you’re working on a shared branch and just want to safely undo something.

Use git reset When You're Still Working Solo

This one’s more aggressive. git reset actually rewinds your branch history to an earlier state.
Only use it if you’re working on a feature branch and no one else has pulled your changes yet.

Here’s how to remove the last commit but keep your code intact:

  git reset --soft HEAD~1
git push --force-with-lease
  

Want to wipe out the commit and the code changes too?

  git reset --hard HEAD~1
git push --force-with-lease  

That --force-with-lease part is important. It makes sure you’re not clobbering someone else’s changes by accident.

You can read more in Git’s official documentation.

Bring Back Deleted Commits Using git reflog

Made a reset mistake and wiped something important? You can usually bring it back with git reflog.

This command shows you every move your HEAD has made recently, even the ones not in git log.

To recover a lost commit:

  git reflog                # find the commit hash
git reset --hard abc123   # go back to that commit
  

Reflog is your safety net. It’s what you use when you’ve messed something up and thought it was gone forever.

Clean Up History with Interactive Rebase

Let’s say your commit history is a mess—multiple “fix typo” or “oops” commits. Before pushing or merging, you might want to clean things up.

That’s where interactive rebase comes in.

Here’s the command:

  git rebase -i HEAD~4
  

It opens a list of your last 4 commits. You can:

  • Change pick to squash to combine commits
  • Change pick to drop to delete them
  • Or edit the messages

After you’re done, force push the cleaned-up history:

  git push --force-with-lease  

Only use this on local branches where history rewriting won’t mess with others.

Quick Recap Table

Tool When To Use Safe for Shared Branches?
revert Undo a pushed commit safely ✅ Yes
reset Rewrite history on a private branch ❌ No
reflog Recover commits after a mistake ✅/❌ Depends
rebase -i Tidy up before pushing or merging ❌ No

Examples (Real Scenarios)

Scenario 1: You pushed a broken commit to main
→ Use git revert and push the fix

Scenario 2: You committed something messy and want to fix before merging
→ Use git reset --soft HEAD~1 or rebase to clean it up

Scenario 3: You ran git reset --hard and lost your work
→ Use git reflog to get it back

FAQs

Can I undo a git push without deleting my changes?

Yes. Use git reset --soft HEAD~1. It keeps your changes in place.

Is git revert safer than reset?

Yes. It never deletes anything and works well on shared branches.

Can I recover deleted commits?

Yes, use git reflog to find and reset back to them.

Do I have to use --force after reset or rebase?

Do I have to use --force after reset or rebase?

Table of Contents

Scroll to Top
Contact Us