Git Reset, Revert, and Checkout Explained โ
Introduction โ
When you need to undo something, choosing the correct command prevents data loss and preserves history integrity. This guide contrasts reset, revert, and checkout (and restore / switch).
Quick Comparison โ
| Command | Scope | History Rewritten? | Common Use |
|---|---|---|---|
| reset | Moves branch refs / staging | Yes (non-public) | Redo local commits |
| revert | New commit undoing prior | No | Public undo |
| checkout (file) | Replace working tree file | No | Discard local changes |
| checkout (branch) | Switch HEAD | No | Change branches |
| restore | Modern file restore | No | Safer file operations |
| switch | Modern branch switch | No | Clarity for branch actions |
Git Reset Modes โ
bash
git reset --soft HEAD~1 # Keep changes staged
git reset --mixed HEAD~1 # (default) Unstage, keep working tree
git reset --hard HEAD~1 # Discard everything (danger!)Use Cases โ
- Soft: Combine last two commits (follow with amend)
- Mixed: Rework commit content / staging
- Hard: Throw away local experimental commits
Moving a Branch Pointer โ
bash
git reset --hard <commit>If already pushedโavoid; use revert instead.
Amending the Last Commit โ
bash
git commit --amend -m "Refine API error handling"If pushed: coordinate or avoid.
Revert (Safe Public Undo) โ
bash
git revert <commit>
git revert <old>..<new> # Revert a rangeCreates inverse patch commit(s), maintaining history.
Reverting a Merge Commit โ
Find its hash then:
bash
git revert -m 1 <merge-hash>-m 1 picks the mainline parent.
Discarding Local File Changes โ
Classic:
bash
git checkout -- path/file.txtModern:
bash
git restore path/file.txt
git restore --staged path/file.txt # UnstageSwitching Branches (Modern Form) โ
bash
git switch main
git switch -c feature/new-dashboardRecovering Lost Commits โ
bash
git reflog
git checkout <lost-hash>Then create a branch:
bash
git switch -c recovery/<topic>Combining Commits (Interactive Rebase) โ
bash
git rebase -i HEAD~5Not strictly an undo, but powerful for rewriting before sharing.
Decision Guide โ
| Situation | Use |
|---|---|
| Undo local last 2 commits (not pushed) | git reset --soft HEAD~2 + amend |
| Undo a public commit | git revert <hash> |
| Discard unstaged file edits | git restore <file> |
| Unstage accidentally added file | git restore --staged <file> |
| Return repo to known good state (local only) | git reset --hard <hash> |
| Explore old commit | git checkout <hash> (detached) |
Safety Tips โ
- Avoid
--hardunless certain (stash first if unsure) - Use revert for anything already shared
- Use branches as safety nets
- Learn
reflogโyour time machine
Summary โ
Choose the least destructive tool that achieves the goal. Reset rewrites; revert records corrective intent; checkout/restore manipulate workspace.
Next Steps โ
- Temporary shelves (
git-stash-and-temporary-changes.md) - Automation (
git-hooks-and-automation.md)
Key Commands
bash
git reset --soft|--mixed|--hard <ref>
git revert <commit>
git restore [--staged] <file>
git reflog