Lesson Completion
Back to course

git reset

Beginner
10 minutes★4.8GitPlay to LearnLearn by Story

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:

ModeCommitsStaging AreaWorking TreeUse Case
--softâ†Šī¸ Undone✅ Kept✅ KeptRecommit differently
--mixed (default)â†Šī¸ UndoneđŸ—‘ī¸ Cleared✅ KeptRe-stage selectively
--hardâ†Šī¸ UndoneđŸ—‘ī¸ ClearedđŸ—‘ī¸ ClearedDiscard 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

flowchart LR subgraph SOFT["--soft"] S_H["History â†Šī¸"] --- S_S["Staging ✅"] S_S --- S_W["Working ✅"] end subgraph MIXED["--mixed"] M_H["History â†Šī¸"] --- M_S["Staging đŸ—‘ī¸"] M_S --- M_W["Working ✅"] end subgraph HARD["--hard"] H_H["History â†Šī¸"] --- H_S["Staging đŸ—‘ī¸"] H_S --- H_W["Working đŸ—‘ī¸"] end style SOFT fill:#1b2d1b,stroke:#53d8fb,color:#53d8fb style MIXED fill:#1a1a2e,stroke:#ffd700,color:#ffd700 style HARD fill:#2d1b1b,stroke:#e94560,color:#e94560

Why It Matters

  • Flexible undo: Each mode gives you different levels of "undo."
  • Commit editing: --soft lets you redo commits (squash, split, reword).
  • Cleanup: --hard gives you a pristine starting point.
  • Local only: Never use reset on commits you've pushed to shared branches.

Code

bash
# ─── 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 messagegit reset --soft HEAD~1 then recommit
Unstage all filesgit reset (same as git reset --mixed HEAD)
Squash last 3 commitsgit reset --soft HEAD~3 then recommit
Discard everything since last commitgit reset --hard HEAD
Go back to a specific commitgit reset --hard <sha>

Key Takeaways

  • --soft preserves everything except the commit → use for recommitting.
  • --mixed (default) clears staging → use for re-staging.
  • --hard clears everything → use when you want to discard all changes.
  • Commits aren't truly deleted — use git reflog to recover from mistakes.

Interview Prep

  • Q: What are the three modes of git reset and when would you use each? A: --soft resets 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. --hard resets everything — use when you want to discard all changes completely.

  • Q: Is git reset safe to use on pushed commits? A: No. git reset rewrites history by moving the branch pointer. If other developers have pulled those commits, resetting and force-pushing will cause their clones to diverge. Use git revert for pushed commits instead.

  • Q: How do you recover from an accidental git reset --hard? A: Use git reflog to find the commit hash before the reset, then run git reset --hard <sha> to restore. The reflog keeps references to all recent HEAD positions for approximately 30 days.

Topics Covered

Git HistoryGit Fundamentals

Tags

#git#reset#undo#beginner-friendly

Last Updated

2026-02-13