BEGINNERGITRECOVERY

Undo the Last Git Commit (Keep or Discard Changes)

How to undo the most recent git commit using git reset, with --soft, --mixed, and --hard explained side by side.

Published Apr 10, 2026
gitresetundorecoveryshell

git reset is how you undo the most recent commit. Which flag you pass decides what happens to the changes inside that commit. This is the snippet I send people who just made a commit they immediately regret.

Tested on git 2.43+.

When to Use This

  • You committed too early and want to add more files
  • You committed to the wrong branch
  • You committed something with a bad message and want to redo it
  • You accidentally committed a secret or large file (use --hard and rotate the secret)

Don't use this when the commit has already been pushed and others may have pulled it. Use git revert instead.

Code

# Undo the last commit, keep all changes staged
git reset --soft HEAD~1
 
# Undo the last commit, keep changes but unstage them (default)
git reset HEAD~1
 
# Undo the last commit and throw away all changes (destructive)
git reset --hard HEAD~1

HEAD~1 means "one commit before HEAD". To undo the last N commits, use HEAD~N.

Usage

A common scenario: you committed feat: add login page but forgot to include the new CSS file.

# 1) Undo the commit but keep your changes staged
git reset --soft HEAD~1
 
# 2) Add the missing file
git add src/styles/login.css
 
# 3) Recommit with everything
git commit -m "feat: add login page"

Another scenario: you committed straight to main instead of a feature branch.

# 1) Move the commit off main, into a new branch
git branch feature/login
git reset --hard HEAD~1   # main is now back where it was
git checkout feature/login

Pitfalls

  • --hard is destructive. It wipes uncommitted changes in your working directory. If you're not sure, run git stash first as insurance.
  • reset only works on local history. If the commit was pushed, anyone who has pulled will see it again on their next pull. Always prefer git revert for shared branches.
  • The commit isn't actually deleted. Reset moves HEAD, but the orphaned commit stays in git reflog for ~90 days. You can recover it with git reset --hard <sha> from the reflog.
  • git commit --amend is often what you really want for fixing the most recent commit's message or adding a forgotten file. It rewrites the last commit in place instead of resetting and re-committing.
  • Recover a Deleted Branch with git reflog(coming soon) — how to find the orphaned commit
  • Interactive Rebase to Squash Commits(coming soon) — for cleaning up multiple commits at once
  • git reset official docs — full flag reference

Frequently Asked Questions

What is the difference between git reset --soft, --mixed, and --hard?

All three move HEAD back to the chosen commit. --soft keeps the staged changes, --mixed (the default) keeps the working directory but unstages the changes, and --hard wipes both staged and working directory changes. --hard is destructive and cannot be undone without the reflog.

Can you undo a commit that has already been pushed?

Yes, but use git revert instead of git reset. revert creates a new commit that undoes the old one, which is safe to push. Resetting and force-pushing rewrites history and breaks anyone who has already pulled.

X (Twitter)LinkedIn