Undo the last commit
Medium risk
What do risk levels mean?
- Read-only
- Inspects state without changing anything.
- Low risk
- Reversible with a routine follow-up command.
- Medium risk
- Changes state; undo path documented.
- Destructive
- Deletes or overwrites; confirmation required.
Moves the current branch back one commit while keeping the removed commit's changes staged, so they can be amended or committed again.
gitcommitundo
Commands
Inspect the working tree before changing branch history
git status --shortRemove the last commit while keeping its changes staged
git reset --soft HEAD~1Verification
git status --shortThe previous commit's changes are staged in the working tree.
Undo
Restore the branch tip to its position before the reset
git reset --soft 'HEAD@{1}'Pitfalls
- Fails in a repository with only one commit because HEAD~1 does not exist.
- Prefer git revert on a shared branch to avoid rewriting published history.