git reset
Rewriting local history with git reset — soft, mixed, and hard modes and when to use each.
What you'll learn
- The three reset modes: --soft, --mixed (default), and --hard
- How reset moves the branch pointer
- Unstaging files with git reset without losing changes
- Why reset is dangerous on shared branches
Concept
What git reset Does
git reset moves the current branch pointer to a different commit. Depending on the mode, it also affects the staging area and working directory.
git reset HEAD~1
This moves the current branch back one commit. The key question is: what happens to your changes?
The Three Modes
--soft (safest)
git reset --soft HEAD~1
Moves the branch pointer back, but keeps your changes staged. The commit is undone, but all its changes are in the staging area, ready to commit again. Useful if you want to re-commit with a different message or combine commits.
--mixed (default)
git reset HEAD~1
git reset --mixed HEAD~1 # same thing
Moves the branch pointer back and un-stages the changes. The changes are still in your working directory (unstaged), but not in the staging area. This is the default behavior.
--hard (most dangerous)
git reset --hard HEAD~1
Moves the branch pointer back and discards all changes — both staged and in the working directory. The commit and its changes are gone. This cannot be undone (unless you find the commit hash in git reflog).
Visual Summary
Original: A---B---C (HEAD, staging, working dir all at C)
reset --soft HEAD~1: A---B (HEAD at B, staging has C's changes, working dir unchanged)
reset --mixed HEAD~1: A---B (HEAD at B, staging empty, working dir has C's changes)
reset --hard HEAD~1: A---B (HEAD at B, staging empty, working dir clean — C's changes LOST)
Unstaging Files
A common, safe use of git reset is to unstage a file without losing the changes:
git add wrong-file.js
git reset wrong-file.js # unstage the file (changes stay in working dir)
# or:
git restore --staged wrong-file.js
This uses the default --mixed mode and is completely safe — no changes are lost.
Resetting to a Remote Branch
If your local branch has diverged badly from the remote, you can reset to match the remote:
git fetch origin
git reset --hard origin/main
Warning: this discards all local commits and changes. Use only when you are sure you want to match the remote exactly.
Recovering from a Bad Reset
If you accidentally ran git reset --hard and lost commits, you can often recover using the reflog:
git reflog
# find the hash of the commit you lost
git reset --hard <lost-commit-hash>
The reflog records every HEAD movement, so even "deleted" commits are recoverable for a while (until garbage collection runs).
When to Use reset vs revert
- Local commits not yet pushed →
git resetis fine - Commits already pushed to a shared branch → use
git revertinstead - Just want to unstage a file →
git reset <file>(safe, default mode) - Want to discard all local changes →
git reset --hard(destructive)
Example
# --- Safe: unstage a file without losing changes ---
git add wrong-file.js
git reset wrong-file.js # changes stay in working dir, just unstaged
# or the newer way:
git restore --staged wrong-file.js
# --- Soft reset: undo last commit, keep changes staged ---
git commit -m "oops, bad message"
git reset --soft HEAD~1
# changes are staged, re-commit with a better message:
git commit -m "feat: add user authentication"
# --- Mixed reset (default): undo last commit, unstage changes ---
git reset HEAD~1
# changes are in working dir (unstaged), stage and commit again:
git add .
git commit -m "feat: add user authentication"
# --- Hard reset: discard the last commit entirely (DANGEROUS) ---
git reset --hard HEAD~1
# the commit and its changes are GONE
# --- Reset to match the remote branch ---
git fetch origin
git reset --hard origin/main
# local main now exactly matches remote main
# --- Recover a lost commit with reflog ---
git reflog
# c3d2e1f HEAD@{0}: reset: moving to HEAD~1
# a1b2c3d HEAD@{1}: commit: feat: add payment
git reset --hard a1b2c3d # recovered!
# --- Reset a specific file to a previous commit ---
git reset abc1234 -- config.js
git checkout -- config.js
Shows all three reset modes, unstaging files safely, resetting to a remote branch, recovering a lost commit via reflog, and resetting a specific file. The reflog recovery is a lifesaver when you accidentally reset --hard.
Try it
- Code Diff
Check what changes a reset will discard before running --hard.
- File Hash Checksum
Git tracks content by hash — reflog uses these to recover commits.
Common mistakes
The mistake
Running git reset --hard on commits that were already pushed
The fix
Once commits are pushed, other developers may have pulled them. reset --hard removes them from history, causing conflicts for everyone. Use git revert instead, which creates a new commit that undoes the changes without rewriting history. Only use reset --hard on local, unpushed commits.
The mistake
Using git reset --hard and losing uncommitted work that was never committed
The fix
git reset --hard discards uncommitted changes with no easy recovery (reflog only tracks commits, not uncommitted work). Before running reset --hard, stash your changes with git stash if you might need them. Or commit them first so they are in the reflog.
Related Resources
Related Cheatsheets
- Git Cheatsheet
reset --soft, --mixed, --hard, and reflog recovery.
Practice git reset
9 exercises
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.