“Undo the last commit” is four different requests. You may want to keep the changes staged, keep them unstaged, discard them, or undo a commit that is already public. The command differs, and one of them can destroy uncommitted work.
The safe first step
Before any reset, check what is in the working tree:
git status
git log --oneline -5
If there are uncommitted changes you want to keep, stash them first:
git stash push -m "before undo"
This is not paranoia. git reset --hard discards uncommitted changes in tracked files, and there is no reflog entry for work that was never committed.
Option 1: keep the changes staged
git reset --soft HEAD~1
The commit is removed, the branch moves back one, and the changes remain in the index. git status shows them as staged. This is the right choice when the commit was premature and you want to recommit with a better message or add another file.
Option 2: keep the changes unstaged
git reset HEAD~1 # --mixed is the default
The commit is removed and the changes remain in the working tree, unstaged. This is useful when you want to review the diff and stage it in smaller pieces.
Option 3: discard the changes
git reset --hard HEAD~1
The commit and the changes are gone from the branch and the working tree. If the commit was already made, it is usually recoverable through the reflog:
git reflog
git reset --hard <sha-before-reset>
Uncommitted changes are not recoverable. This is the option that requires a stash or a backup.
Option 4: undo a public commit
If the commit is already pushed to a shared branch, do not reset. Rewriting shared history forces everyone else to recover. Use revert:
git revert HEAD
This creates a new commit that applies the inverse of the original. History is preserved, collaborators are unaffected, and the change is auditable. For a merge commit, specify the mainline:
git revert -m 1 <merge-sha>
Amend the message or contents
If the commit is the latest one and has not been pushed:
git commit --amend -m "better message"
git commit --amend --no-edit # keep the message, add staged changes
Amend rewrites the commit ID. Do not amend a commit that others have based work on unless you coordinate the rewrite.
Restore a single file from the last commit
git restore path/to/file # discard working-tree changes
git restore --staged path/to/file # unstage, keep changes
git restore --source=HEAD~1 path # bring back the previous version
git checkout -- file does the same as the first command in older Git versions, but restore is clearer about intent.
Recover a commit that seems lost
The reflog records every position of HEAD:
git reflog --date=relative
Find the commit before the reset or rebase and reset to it, or create a branch at that point:
git branch rescue <sha>
The reflog is local and expires (90 days by default for reachable entries, 30 for unreachable). It is not a backup strategy for a shared remote.
A decision table
| Goal | Command | Rewrites history |
|---|---|---|
| Recommit with changes staged | git reset --soft HEAD~1 |
Yes |
| Review changes before recommitting | git reset HEAD~1 |
Yes |
| Throw the commit away | git reset --hard HEAD~1 |
Yes |
| Undo a pushed commit | git revert HEAD |
No |
| Fix the message | git commit --amend |
Yes |
| Undo only a file | git restore |
No |
The rule of thumb: reset before push, revert after push. Everything else is a detail about what happens to the index and the working tree.
For the full set of recipes, see the Git undo commit page and the revert page.