Undo a Git commit: reset, revert and restore
Committed too quickly? Pushed a bug? Don't panic. Git offers several ways to undo. This page explains which one to use depending on your situation.
gitquest — enquête
Which command should you use?
Uncommitted changes
git restore
You modified files but haven't committed yet. You want to go back to the previous version.
git restore <file>Local commit (not pushed)
git reset
You committed but haven't pushed yet. You want to undo the commit (with or without the changes).
git reset --soft HEAD~1Already pushed commit
git revert
The commit is already shared. You want to undo it cleanly without rewriting history.
git revert <hash>The 3 methods in action
Each method illustrated with a concrete example in the terminal.
git reset --soft
gitquest — reset
# Undo the last commit while keeping changes
$git log --oneline -3
a1b2c3d (HEAD -> main) Commit too quickly
f4e5d6c Add config file
b7c8d9e Initial commit
$git reset --soft HEAD~1
HEAD is now at f4e5d6c
$git status
Changes to be committed: your files are in staging
git revert
gitquest — revert
# Undo a commit by creating a reverse commit (safe)
$git log --oneline -3
a1b2c3d (HEAD -> main) Add buggy function
f4e5d6c Add config file
$git revert a1b2c3d
[main c3d4e5f] Revert "Add buggy function"
The commit is undone without modifying the history!
git restore
gitquest — restore
# Undo uncommitted changes
$git status
Unstaged changes:
modified: index.html
$git restore index.html
The file is back to its last committed version
# Unstage a file (without losing changes)
$git restore --staged style.css
The file is unstaged but changes are preserved
Comparison: reset vs revert vs restore
| Criteria | git reset | git revert | git restore |
|---|---|---|---|
| Acts on | Commits | Commits | Files |
| Rewrites history | Yes | No | No |
| Safe for shared branch | No | Yes | Yes |
| Keeps changes | Depends on mode (--soft/--hard) | Creates a new commit | Discards changes |
| Use case | Local commit to fix | Pushed commit to undo | Modified file to restore |
A
GitQuest is created by Anaïs (nouvelle fenêtre), web developer and head of education, specializing in tech training and digital accessibility.
Questions about undoing commits
Learn to undo without panicking
GitQuest trains you to master reset, revert and restore in realistic situations.
Start practicing now