What started as a routine cleanup became a lesson in Git's resilience — and a reminder that understanding the model matters more than memorizing commands.
The Moment Everything Disappeared
I had a feature branch with a freshly committed blog post. I was tidying up my local repository — something I'd done a hundred times before. Then, almost on autopilot:
git checkout blogs/microservices-by-def
git reset --hard 65515962bc35fe08514f0b1dcad58cb89773bd2d
The terminal didn't flinch. No warning. No confirmation prompt.
My article was gone. Hours of writing — vanished in under a second.
What Actually Happened
Before: A ── B ── C ── D ← my blog commit
After: A ── B ── C ← pointer moved here (D still exists, orphaned)
Git didn't delete my commit. It just moved the branch pointer. The commit was still there — floating, unreachable, but alive.
The Mental Model That Changes Everything
Git doesn't store files. It stores snapshots. A branch is just a pointer. git reset relocates that pointer — it doesn't destroy history.
The commit persists until garbage collection prunes unreachable objects (which doesn't happen immediately).
The Recovery: git reflog
git reflog show blogs/microservices-by-def
6551596 reset: moving to 65515962bc35...
0434e98 commit: added blog on Microservices by Default
Recovery took one command:
git reset --hard 0434e98
Everything came back.
The Three Reset Modes
| Command | Working Tree | Index | HEAD |
|---|---|---|---|
git reset --soft |
✗ | ✗ | ✓ |
git reset --mixed |
✗ | ✓ | ✓ |
git reset --hard |
✓ | ✓ | ✓ |
--hard rewrites all three areas. That's why my files disappeared.
Quick Decision Matrix
| I want to... | Use |
|---|---|
| Undo a local commit | git reset |
| Undo a pushed commit | git revert |
| Recover lost work | git reflog |
| Save work temporarily | git stash |
| Restore a single file | git restore |
| Clean up commit history | git rebase -i |
Key Takeaways
- Commit early, commit often. Small commits are cheap insurance.
- Push important milestones. Remote refs survive local disasters.
-
Learn
reflogbefore you need it. In a panic, you won't have time to read docs. - Never panic after a bad Git command. Most mistakes are recoverable.
The full post covers merge vs rebase, interactive rebase, reword, cherry-pick, stash, git fsck, and why linear history matters for CI/CD.
Top comments (0)