Git Undo Lab: Reset, Revert, Restore, and Reflog
Understand the difference between git restore, git reset --soft, git reset --mixed, git reset --hard, git revert, and git reflog. The safe undo lab shows how each command changes the working tree, staging area, branch pointer, and commit history.
Use git revert for a public history-preserving undo, and learn when reset or reflog is appropriate for local recovery.
Complete Git Undo Comparison
| Command | Type | Safety | Description | Working Directory | Index | History | Warning |
|---|---|---|---|---|---|---|---|
| git restore <file> | Working Tree Undo | Caution | Discards unstaged modifications in the working directory, restoring the file to match the index/HEAD. | Discards unstaged edits | Unchanged | Preserved (no new commit) | Destructive for unstaged edits! Edits made in the working tree that were never staged will be lost permanently. |
| git restore --staged <file> | Unstage from Index | Safe | Unstages a file from the Index back to the working directory without losing any edits. | Preserves your edits in working directory | Clears staged version back to HEAD | Preserved | |
| git reset --soft HEAD~1 | Undo Commit (Keep Staged) | Safe | Moves branch pointer back by 1 commit. Leaves all changes staged in the Index ready to re-commit. | Completely preserved | Changes remain staged in index | Rewrites branch tip (reflog keeps old hash) | Safe locally. If the commit was already pushed to a shared remote, rewriting the branch tip can cause sync issues for teammates. |
| git reset [--mixed] HEAD~1 | Undo Commit (Keep Unstaged) | Safe | Moves branch pointer back and un-stages files from Index. All changes are kept as unstaged files in your working directory. | Preserved as unstaged files | Reset to match HEAD~1 | Rewrites branch tip | Your changes are not lost; they are kept unstaged in your working directory. |
| git reset --hard HEAD~1 | Nuclear Reset | Destructive | Moves branch pointer back AND forcibly overwrites both Index and Working Directory to match HEAD~1. | Discarded permanently | Discarded permanently | Rewrites branch tip | High risk: All uncommitted modifications in your working directory and staging area will be permanently destroyed. Committed work can only be recovered via reflog. |
| git revert <commit> | Safe Public History Undo | Safest | Creates a brand-new commit that inverts the changes of the target commit. Does not delete or rewrite past history. | Updated with inverse diff | Committed cleanly | Fully preserved (adds new commit) | The recommended way to undo work on shared or team branches because it does not alter past commit hashes. |
| git reflog & recovery | Emergency Recovery Net | Safe | Logs every HEAD position visited. Allows recovering lost commits after accidental reset or branch deletion. | Read-only inspection | Unchanged | Reveals all previous commit hashes | Reflog entries are local only and expire after 90 days by default. |
Explore Git Visualizer
- Interactive Git Sandbox and Commit Graph
- Guided Git Lessons and Challenges
- Git Undo Lab
- How Git Works Architecture Flowchart
- Git Command Reference
Git Visualizer is created by Dipesh Sapkota.