How to Undo the Last Git Commit: reset, revert, amend and reflog
Undo a Git commit safely whether or not you have pushed it — keep or discard the changes, fix the message, remove a file, and recover from a bad reset with reflog.
By Software Side Hustles Editorial Team · Published · Updated · 5 min read
Quick answer: if you have not pushed, run git reset --soft HEAD~1 to undo the commit and keep the changes staged, or git reset HEAD~1 to keep them unstaged. If you have pushed to a shared branch, run git revert HEAD to create a new commit that reverses it. Only use git reset --hard HEAD~1 when you want to throw the changes away.
Pick the right command
| Situation | Command | Your changes |
|---|---|---|
| Wrong commit message | git commit --amend -m "Better message" | Kept, same commit replaced |
| Forgot a file | git add file && git commit --amend --no-edit | Added to the last commit |
| Undo commit, keep changes staged | git reset --soft HEAD~1 | Staged |
| Undo commit, keep changes unstaged | git reset HEAD~1 (--mixed) | In working tree |
| Undo commit and discard changes | git reset --hard HEAD~1 | Deleted |
| Undo a commit that is already pushed | git revert HEAD | Reversed by a new commit |
| "I reset and lost my work" | git reflog, then git reset --hard <sha> | Recovered |
HEAD~1 means "one commit before the current one". Use HEAD~3 to go back three commits.
How do I undo a commit that I haven't pushed?
Because the commit only exists on your machine, you can rewrite history freely.
git reset --soft HEAD~1The commit disappears, and every change it contained is back in the staging area, ready to recommit. This is the safest form of reset — nothing is lost.
git reset HEAD~1 # same as --mixedAlso removes the commit, but leaves the changes unstaged in your working tree. Use it when you want to split the work into several smaller commits.
git reset --hard HEAD~1Removes the commit and discards its changes from your working tree. Any uncommitted edits are lost as well. Run git status and git stash first if you are not completely sure.
How do I undo a commit that I already pushed?
Rewriting history that other people have pulled causes duplicated commits and confusing conflicts for them. On a shared branch, add a new commit that undoes the old one:
git revert HEAD # reverse the latest commitgit revert abc1234 # reverse a specific commitgit pushgit revert is safe because it only adds history. The original commit stays visible, which is usually what you want on main anyway: there is a clear record of what was undone and why.
If the branch is yours alone — a feature branch nobody else uses — you can reset locally and force-push:
git reset --hard HEAD~1git push --force-with-lease--force-with-lease refuses to overwrite the remote if someone else pushed in the meantime, which makes it strictly safer than --force.
How do I change the last commit message?
git commit --amend -m "Fix pagination off-by-one in search results"--amend replaces the last commit with a new one. It has a new hash, so the same "already pushed?" rule applies: amending a pushed commit requires a force-push.
How do I remove a file from the last commit?
git rm --cached secrets.env # stop tracking, keep the file on diskecho "secrets.env" >> .gitignoregit commit --amend --no-editIf the file contained a secret and you already pushed, treat the secret as compromised and rotate it. Removing it from the latest commit does not remove it from history, forks, clones or CI logs. Rewriting history with git filter-repo helps, but rotation is the real fix.
How do I recover a commit after a bad reset?
Git keeps a log of where HEAD has been, even for commits no branch points to any more:
git reflog# a1b2c3d HEAD@{0}: reset: moving to HEAD~1# 9f8e7d6 HEAD@{1}: commit: Add invoice exportgit reset --hard 9f8e7d6Reflog entries are kept for a while (90 days by default for reachable entries, 30 for unreachable ones), so "lost" commits are usually recoverable. Uncommitted changes discarded by reset --hard or checkout -- file are a different story — Git never stored them. Commit or stash early.
Undoing other things
- Unstage a file:
git restore --staged file(older Git:git reset HEAD file). - Discard local edits to a file:
git restore file. This cannot be undone. - Undo a merge you haven't pushed:
git reset --hard ORIG_HEAD. - Undo a pushed merge:
git revert -m 1 <merge-sha>, where-m 1keeps the first parent (usuallymain). - Abort a merge or rebase in progress:
git merge --abortorgit rebase --abort. See how to resolve merge conflicts. - Committed to the wrong branch:
git branch feature-x(keep the work), thengit reset --hard HEAD~1on the wrong branch, thengit switch feature-x.
Frequently asked questions
What is the difference between git reset and git revert?
reset moves the branch pointer backwards, removing commits from the branch's history. revert adds a new commit that applies the opposite changes. Use reset for local, unpushed work; use revert for anything shared.
Does git reset delete my files?
--soft and --mixed never touch your working files. --hard overwrites them to match the target commit.
How do I undo git add?
git restore --staged <file>, or git restore --staged . for everything.
How do I undo the last N commits?
git reset --soft HEAD~N to combine them into one set of staged changes, or git revert HEAD~N..HEAD to reverse them on a shared branch.
Where this skill pays off
Choosing between reset and revert depends on context — exactly the kind of judgement AI assistants get wrong when they suggest git push --force to someone on a shared branch. Evaluating whether an AI's advice is safe, not just technically correct, is a big part of paid AI evaluation work; see common mistakes when reviewing AI-generated code.