bashbeginner

Git Reset — Soft, Mixed, and Hard Explained

Understand the three git reset modes and when to use each for undoing commits safely.

bash
# === SOFT reset ===
# Moves HEAD back but keeps changes staged (ready to re-commit)
git reset --soft HEAD~1
# Use case: redo the last commit message or combine commits
git commit -m "better commit message"

# === MIXED reset (default) ===
# Moves HEAD back, unstages changes (keeps in working directory)
git reset HEAD~1     # same as: git reset --mixed HEAD~1
# Use case: undo commit but keep editing files

# === HARD reset ===
# Moves HEAD back AND discards all changes (destructive!)
git reset --hard HEAD~1
# Use case: completely undo last commit and changes

# Reset to a specific commit
git reset --soft abc1234

# Reset a single file (unstage it)
git reset HEAD -- path/to/file.ts

# Comparison table:
# Mode     | HEAD | Index/Stage | Working Dir
# ---------|------|-------------|------------
# --soft   | YES  | no          | no
# --mixed  | YES  | YES         | no
# --hard   | YES  | YES         | YES

# Safety net: check reflog before hard reset
git reflog
# If you regret a hard reset:
git reset --hard HEAD@{1}   # go back to pre-reset state

Use Cases

  • Undoing accidental commits before pushing
  • Unstaging files added by mistake
  • Starting fresh from a known-good commit

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.