The Hook (The "Byte-Sized" Intro)
git reset is a time machine with three settings: soft (gentle undo), mixed (regular undo), and hard (nuclear undo). Choose wrong and you lose work. Choose right and you get a clean slate exactly where you need it. The trick is knowing which of Git's three areas â commit history, staging area, working tree â each mode affects.
đ What is git reset?
git reset moves the current branch pointer to a different commit. Depending on the mode, it also optionally clears the staging area and working tree. It's the primary tool for undoing commits locally.
Conceptual Clarity
The three modes:
| Mode | Commits | Staging Area | Working Tree | Use Case |
|---|---|---|---|---|
--soft | âŠī¸ Undone | â Kept | â Kept | Recommit differently |
--mixed (default) | âŠī¸ Undone | đī¸ Cleared | â Kept | Re-stage selectively |
--hard | âŠī¸ Undone | đī¸ Cleared | đī¸ Cleared | Discard everything |
What "undo" means here:
The commits aren't deleted â the branch pointer simply moves backward. The commits still exist in Git's object database (recoverable via reflog for ~30 days).
Real-Life Analogy
--soft= Taking the envelope back from the mailbox (letter still written and sealed)--mixed= Taking the letter out of the envelope too (letter still written, not sealed)--hard= Shredding the letter entirely (gone)
Visual Architecture
Why It Matters
- Flexible undo: Each mode gives you different levels of "undo."
- Commit editing:
--softlets you redo commits (squash, split, reword). - Cleanup:
--hardgives you a pristine starting point. - Local only: Never use
reseton commits you've pushed to shared branches.
Code
# âââ Soft reset: undo commit, keep staged + working âââ
git reset --soft HEAD~1
# Your changes are still staged â ready to recommit
git commit -m "Better commit message"
# âââ Mixed reset (default): undo commit + staging âââ
git reset HEAD~1
# Changes are in working tree but unstaged
git add specific-file.js # Re-stage selectively
git commit -m "Targeted commit"
# âââ Hard reset: undo everything âââ
git reset --hard HEAD~1
# â ī¸ All changes GONE (commit, staging, working tree)
# âââ Reset to a specific commit âââ
git reset --soft abc1234
# âââ Undo the last 3 commits (keep changes) âââ
git reset --soft HEAD~3
git commit -m "Combined 3 commits into one"
# âââ Recovery: if hard reset was a mistake âââ
git reflog
# Find the commit before the reset
git reset --hard HEAD@{1}
# You're back!Quick Reference
| Want to... | Command |
|---|---|
| Redo the last commit message | git reset --soft HEAD~1 then recommit |
| Unstage all files | git reset (same as git reset --mixed HEAD) |
| Squash last 3 commits | git reset --soft HEAD~3 then recommit |
| Discard everything since last commit | git reset --hard HEAD |
| Go back to a specific commit | git reset --hard <sha> |
Key Takeaways
--softpreserves everything except the commit â use for recommitting.--mixed(default) clears staging â use for re-staging.--hardclears everything â use when you want to discard all changes.- Commits aren't truly deleted â use
git reflogto recover from mistakes.
Interview Prep
-
Q: What are the three modes of
git resetand when would you use each? A:--softresets the commit but keeps staged and working tree changes â use when you want to recommit differently.--mixed(default) resets commit and staging â use when you want to re-stage files selectively.--hardresets everything â use when you want to discard all changes completely. -
Q: Is
git resetsafe to use on pushed commits? A: No.git resetrewrites history by moving the branch pointer. If other developers have pulled those commits, resetting and force-pushing will cause their clones to diverge. Usegit revertfor pushed commits instead. -
Q: How do you recover from an accidental
git reset --hard? A: Usegit reflogto find the commit hash before the reset, then rungit reset --hard <sha>to restore. The reflog keeps references to all recent HEAD positions for approximately 30 days.