How to Undo the Last Git Commit (Every Case)
You committed too early, wrote a bad message, or included the wrong file. Here's the decision tree for undoing a Git commit safely, by what you actually want to keep.
Key takeaways
- To undo the last Git commit and keep the work staged, run git reset --soft HEAD~1; use git reset HEAD~1 to keep it unstaged, and git reset --hard HEAD~1 only to delete it.
- If the commit is already pushed to a shared branch, run git revert HEAD instead. git reflog recovers commits you reset away.
On this page
To undo the last commit and keep your changes, run git reset --soft HEAD~1 (changes stay staged) or git reset HEAD~1 (changes stay unstaged). To throw the work away, git reset --hard HEAD~1. If the commit is already pushed to a shared branch, use git revert HEAD instead.
Verified against
Git 2.56.0, against the git-scm.com reference pages for git-reset, git-revert, git-reflog and git-gc. Checked October 2026.
Almost everyone gets scared off by an answer that shows git reset --hard without saying what it deletes. So answer the keep-or-discard question first, and the rest is easy. Here's the fast map. Read your case, run the command.
You want to keep the changes, staged and ready
You committed too early. The code is fine, you just want it back out of the commit and sitting in the staging area so you can regroup.
git reset --soft HEAD~1
This moves the branch pointer back one commit and leaves the index and working tree untouched, exactly as they were the moment before you typed git commit. Nothing is lost. Run git status right after and you'll see your files under "Changes to be committed." This is the gentlest undo Git has, and it's the one we use most.
You want to keep the changes, but unstaged
Same as above, except you also want the files pulled out of staging so you can pick and choose what goes into the next commit.
git reset --mixed HEAD~1
# or just:
git reset HEAD~1
--mixed is the documented default mode, so the bare git reset HEAD~1 does the same thing. The commit is gone, the changes are back in your working directory as modified files, and staging is clean. You lose nothing but the commit itself. This is the right call when you committed a big blob and want to split it into two.
You want the changes gone entirely
The commit was garbage and so is the work in it. You want the tree back to how it looked one commit ago, with no trace.
git reset --hard HEAD~1
Read this line twice before running it. --hard throws away the commit AND every uncommitted change to tracked files in your working directory. Not just the last commit's changes: everything that isn't committed. If you had unrelated edits open, they go too. There is no undo prompt. The only safety net is the reflog, and only if the work was committed at some point.
Our rule: never run --hard on a dirty tree you haven't looked at. Run git status first. If there's anything in there you can't afford to lose, stash it before you reset.
You just need to fix the message or add a forgotten file
Don't reset for this. The commit is fine, you just fumbled it.
# fix the message
git commit --amend -m "the message you meant to write"
# forgot to stage a file? add it, then fold it in
git add forgotten-file.js
git commit --amend --no-edit
--amend rewrites the last commit in place. --no-edit keeps the existing message while pulling in whatever's now staged. Clean and quick. One catch: amend creates a new commit with a new hash, which matters the moment the old one is already pushed. Hold that thought.
The commit is already pushed
This is where people get hurt, so here's the rule with no hedging: if the commit is on a shared branch that other people pull, use revert, not reset.
git revert HEAD
revert doesn't erase history. It creates a new commit that undoes the changes of the target commit. The bad change is neutralized, but the timeline stays intact, so nobody else's clone breaks. Push it like a normal commit and you're done. If you want the deeper contrast on why this beats rewriting, we wrote up revert vs reset separately.
You can still reset a pushed commit and force-push over it:
git reset --hard HEAD~1
git push --force-with-lease
But understand the trade. Anyone who already pulled that commit now has history that disagrees with the remote, and their next pull turns into a mess you'll be untangling in Slack. Use --force-with-lease instead of plain --force so the push refuses if someone else has pushed in the meantime. On a solo feature branch nobody else touches, a force-push is fine and honestly cleaner. On main or anything shared, revert. If the remote then rejects your normal push, our guide to fixing a rejected git push covers the follow-up.
Undoing more than one commit
The HEAD~1 is just "one commit back." Change the number to reach further:
git reset --soft HEAD~3 # undo last 3 commits, keep all their changes staged
git reset --hard HEAD~3 # undo last 3 commits and delete their changes
For pushed commits, revert a range instead:
git revert --no-commit HEAD~3..HEAD
git commit -m "revert the last three commits"
HEAD~3..HEAD covers the last three commits (the range excludes HEAD~3 itself). --no-commit stacks the reversals so you get one clean commit instead of three.
You reset too far and lost a commit
This is the part nobody tells you, and it's the reason reset is less scary than it looks. Git keeps a log of everywhere HEAD has been, even after a hard reset.
git reflog
You'll get a list like a1b2c3d HEAD@{1}: commit: the thing you thought you nuked. Find the hash of the commit you want back and either check it out or reset to it:
git reset --hard a1b2c3d
How long that net holds depends on reachability. Per the git-gc docs, gc.reflogExpire defaults to 90 days, but gc.reflogExpireUnreachable (entries no longer reachable from the current branch tip, which is exactly what a reset-away commit is) defaults to 30 days. So a commit you reset away is recoverable for about a month by default. The one thing the reflog can't save is work that was never committed, changes that only ever lived in your working directory before a --hard. That's the real reason to commit early and often: a commit is recoverable, an unsaved edit is not.
If you get stuck on something outside this tree, our broader Git troubleshooting guide covers the neighboring failures like rejected pushes and detached HEAD.
Questions people ask
How do I undo the last commit but keep my changes?
Run git reset --soft HEAD~1 to keep the changes staged, or git reset HEAD~1 to keep them as unstaged modifications. Both remove only the commit; your files are untouched. Use the soft form when you just want to re-commit with a different message or grouping, and the mixed form when you want to split the work into several smaller commits.
How do I undo a commit that is already pushed?
On a shared branch, run git revert HEAD and push the result. It adds a new commit that reverses the bad one, so nobody else's history breaks. Only on a branch nobody else uses should you git reset and then git push --force-with-lease, which refuses to overwrite the remote if someone pushed in the meantime.
Can I recover a commit after git reset --hard?
Usually, yes. Run git reflog, find the hash of the lost commit, and run git reset --hard <hash> or git branch rescue <hash>. Unreachable reflog entries expire after 30 days by default. What you cannot recover is uncommitted work: anything only in your working tree when you ran --hard is gone.
The call we'd make
Ask one question before you touch anything: is this commit pushed to a branch other people use? If yes, git revert and move on, no exceptions worth the risk. If no, pick your reset by what you want to keep: --soft to hold the work staged, default --mixed to hold it unstaged, --hard only after git status confirms there's nothing in the working tree you'll cry over. And keep git reflog in your back pocket, because the safety net has been there the whole time.
Sources
Cite this article
Kiril Urbonas (2026). How to Undo the Last Git Commit (Every Case). DevOpsNess. Updated 2026-10-31. https://www.devopsness.com/blog/git-undo-last-commit
[How to Undo the Last Git Commit (Every Case)](https://www.devopsness.com/blog/git-undo-last-commit) (Kiril Urbonas, DevOpsNess, updated 2026-10-31)
More from DevOps
Explore more articles in this category
CSRF Protection Done Right
A practical guide to defending against CSRF with SameSite cookies, synchronizer tokens, and double-submit, plus knowing when you don't need any of it.
Kubernetes NetworkPolicies in Practice
Default-deny, namespace isolation, egress control: the patterns we use, the gotchas around DNS, and where Cilium changed our calculus.
Autonomous CI/CD Pipelines: Self-Healing and AI-Assisted Deployments
Evolve CI/CD toward autonomous pipelines that detect issues and roll back safely.
You might have missed
Evergreen posts worth revisiting.