bashbeginner
Git Reset — Soft, Mixed, and Hard Explained
Understand the three git reset modes and when to use each for undoing commits safely.
bashPress ⌘/Ctrl + Shift + C to copy
# === 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 stateUse 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.
bashbeginner
Undo Last Git Commit (Soft and Hard)
Git commands to undo the last commit while keeping changes staged, unstaged, or fully discarded.
Best for: Fixing a commit with wrong files or message
#git#undo
bashintermediate
Git Cherry-Pick Commits Example
Apply specific commits from one branch to another using cherry-pick with conflict resolution.
Best for: Backporting bug fixes to release branches
#git#cherry-pick
bashbeginner
Git Revert Commit Safely
Create a new commit that undoes changes from a previous commit without rewriting history.
Best for: Undoing a deployed commit on shared branches
#git#revert
bashbeginner
Git Reset and Restore File Changes
Commands to discard, unstage, or restore file changes using git restore and git checkout.
Best for: Discarding experimental changes in files
#git#reset