Git reset Command
In this page:
What is Git Reset?
git reset rewinds your current branch to point at an earlier commit, effectively undoing the commits after that point as far as the branch history is concerned. Unlike git revert, it changes history rather than adding a new commit on top, which is powerful but riskier on shared branches.
Example: What is Git Reset?
git reset a1b2c3d
Soft Reset
--soft moves only the branch pointer back — your staged changes and working directory files stay exactly as they were, so everything from the "undone" commits reappears as staged changes ready to be re-committed differently. This is the safest reset mode and a common way to squash several small commits into one.
Example: Soft Reset
git reset --soft HEAD~1
Mixed Reset (Default)
The default mode (no flag, or --mixed) moves the branch pointer back and also empties the staging area, but leaves your working directory files untouched — so your edits survive, they're just no longer staged. Use this when you want to re-review and re-stage changes before committing again.
Example: Mixed Reset (Default)
git reset HEAD~1
git reset --mixed HEAD~1
Hard Reset (Dangerous)
--hard moves the branch pointer back AND overwrites both the staging area and your working directory to match that commit exactly, permanently discarding any uncommitted work in the process. There's no confirmation prompt, so double-check you actually want to lose those changes before running it.
Example: Hard Reset (Dangerous)
git reset --hard HEAD~1
Recovering from Reset
Even after a hard reset, the commits you "lost" usually aren't gone — Git's reflog keeps a record of where your branch tip has pointed recently, so you can look up the old commit hash and reset back onto it. This safety net is why hard resets, while scary, are rarely truly catastrophic within the reflog's retention window.
Example: Recovering from Reset
git reflog
git reset --hard a1b2c3d
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: