Git Notes
Master git reset for undoing commits, unstaging files, and moving HEAD. Understand soft, mixed, and hard reset modes with practical examples.
Git reset is one of the most powerful and potentially dangerous commands in Git. It moves the HEAD pointer backward in history and can optionally modify your staging area and working directory. Understanding the three modes of reset is essential because choosing the wrong one can either save your workflow or destroy uncommitted work.
Think of reset as a time machine with three settings. The "soft" setting takes you back in time but keeps all your luggage. The "mixed" setting takes you back and unpacks your bags. The "hard" setting takes you back and burns everything you were carrying.
The Three Modes Explained
| Mode | HEAD/Branch | Staging Area | Working Directory |
|---|---|---|---|
| --soft | ✅ Moves | ❌ Unchanged | ❌ Unchanged |
| --mixed | ✅ Moves | ✅ Reset | ❌ Unchanged |
| --hard | ✅ Moves | ✅ Reset | ✅ Reset (DANGEROUS) |
All three modes move the branch pointer. The difference is what happens to your actual files.
Soft Reset: Keep Everything Staged
A soft reset moves HEAD but leaves both your staging area and working directory completely untouched. The changes from the "undone" commits end up in your staging area, ready to be recommitted:
git reset --soft HEAD~1This is perfect for combining multiple commits into one:
# You made three small commits that should be one
git log --oneline
# c3 fix: typo in validation
# c2 feat: add form validation
# c1 feat: add login form
# Squash all three into one
git reset --soft HEAD~3
git commit -m "feat: implement complete login form with validation"
# Now you have one clean commit instead of three messy onesAnother use case is when you committed to the wrong branch:
# Oops, committed to main instead of feature branch
git reset --soft HEAD~1 # Undo commit, keep changes staged
git checkout -b feature/login # Create correct branch
git commit -m "feat: add login" # Recommit on correct branchMixed Reset: The Default
When you run git reset without specifying a mode, you get mixed reset. It moves HEAD and resets the staging area, but your working directory files remain unchanged:
git reset HEAD~1
# Same as: git reset --mixed HEAD~1The most common use for mixed reset is unstaging files:
# You accidentally staged everything with git add .
git reset HEAD # Unstage all files
git reset HEAD file.txt # Unstage just one file
# Modern alternative: git restore --staged file.txtMixed reset is also useful when you want to reorganize your commits:
# You made one big commit with unrelated changes
git reset HEAD~1
# All changes back in working directory, unstaged
# Now commit them properly in separate logical commits
git add src/auth/
git commit -m "feat: implement authentication"
git add src/styles/
git commit -m "style: update component styles"
git add tests/
git commit -m "test: add unit tests for auth"Hard Reset: The Nuclear Option
Hard reset moves HEAD and wipes both the staging area and working directory to match the target commit. Any uncommitted changes are permanently lost:
git reset --hard HEAD~1Use cases for hard reset:
# Throw away all local changes and match the remote
git reset --hard origin/main
# Completely undo last 3 commits and all their changes
git reset --hard HEAD~3
# Start fresh from a specific commit
git reset --hard abc1234Critical warning: Hard reset destroys uncommitted work permanently. Unlike commits which can be recovered via reflog, uncommitted changes in your working directory have no safety net.
Practical Scenarios
Scenario 1: Undo a commit but rework the changes
# You committed prematurely - code needs more work
git reset HEAD~1 # Mixed: changes back in working dir
# Edit files, improve the code...
git add .
git commit -m "feat: properly implement user search"Scenario 2: Throw away a failed experiment
# You tried something that completely failed
git reset --hard HEAD~2 # Discard last 2 commits entirely
# Back to a clean stateScenario 3: Sync local branch with remote after force push
# Teammate force-pushed to the branch, your local is out of sync
git fetch origin
git reset --hard origin/feature/shared-branch
# Now your local matches the remote exactlyScenario 4: Unstage specific files before committing
git add . # Staged everything
git reset HEAD secrets.env # Unstage the secrets file
git reset HEAD debug.log # Unstage the debug log
git commit -m "feat: add config" # Commit without sensitive filesReset with Path: File-Level Reset
You can reset individual files without moving HEAD:
# Unstage a file (does NOT move HEAD)
git reset HEAD path/to/file.js
# Restore staging area version of file from a specific commit
git reset abc1234 -- path/to/file.js
# File is now staged with the version from abc1234File-level reset only affects the staging area. It is equivalent to copying the file from the specified commit into the index without touching your working directory.
Recovering from Hard Reset
If you accidentally hard-reset and lost commits, the reflog is your lifeline:
# Find the commit before the reset
git reflog
# e4f5g6h HEAD@{1}: commit: important work <- This is what you need
# a1b2c3d HEAD@{0}: reset: moving to HEAD~3
# Recover by resetting back
git reset --hard e4f5g6h
# Or create a branch to inspect before deciding
git branch recovery e4f5g6h
git log recovery --onelineRemember: reflog only saves committed work. If you had uncommitted changes in your working directory when you ran git reset --hard, those changes are truly gone.
Reset vs Revert: When to Use Each
| Situation | Use Reset | Use Revert |
|---|---|---|
| Commit is local only | ✅ Safe | ✅ Works but unnecessary |
| Commit is pushed/shared | ❌ Never | ✅ Always |
| Want clean history | ✅ Removes evidence | ❌ Adds revert commit |
| Team collaboration | ❌ Breaks others | ✅ Safe for everyone |
| Emergency rollback | ❌ Requires force push | ✅ Simple and fast |
Common Mistakes
Mistake 1: Using git reset --hard when you meant --soft. Always double-check the mode flag. If in doubt, use --soft first — you can always throw away changes later, but you cannot recover them after --hard.
Mistake 2: Resetting commits that have been pushed to shared branches. This rewrites public history and forces teammates to deal with divergent histories.
Mistake 3: Forgetting that uncommitted changes are lost with hard reset. Always run git status and git stash before a hard reset if you have any work in progress.
Interview Questions
Q1: What are the three modes of git reset and how do they differ?
Soft reset moves HEAD only, keeping both staging and working directory intact. Mixed reset (the default) moves HEAD and clears the staging area, but working directory is preserved. Hard reset moves HEAD and wipes both staging area and working directory to match the target commit. The key difference is how much data you keep versus discard.
Q2: How do you undo the last commit but keep the changes?
Use git reset --soft HEAD~1 to keep changes staged and ready to recommit, or git reset HEAD~1 (mixed) to keep changes in the working directory but unstaged. Both preserve your actual code modifications while removing the commit from history.
Q3: What is the difference between git reset and git revert?
Reset moves HEAD backward, rewriting history by making commits unreachable. It is destructive to shared history. Revert creates a new commit that undoes changes from a specific commit, preserving all history. Reset is for local cleanup; revert is for public branches where history must remain intact.
Q4: Can you recover from a hard reset?
Committed work can be recovered using git reflog to find the pre-reset commit hash, then git reset --hard <hash> to restore. However, uncommitted changes in the working directory at the time of hard reset are permanently lost with no recovery mechanism.
Q5: When should you NEVER use git reset?
Never use reset on commits that have been pushed to shared branches that others are working on. It rewrites history, causing force-push requirements and breaking teammates' local repositories. Use git revert for public commits instead.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Git Reset - Move HEAD and Undo Commits.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, undoing, changes, reset, git reset - move head and undo commits
Related Git & GitHub Topics