Engineering Craft › Version Control (Git)
Reset
Moving a branch pointer back, in soft, mixed or hard mode.
Also known as: git reset, reset --hard, reset --soft
git reset moves the current branch back to an earlier commit, and optionally changes the staging area and your files to match. It rewrites where your branch points, so use it on local work.
git reset --soft HEAD~1 # undo the last commit; keep changes staged
git reset HEAD~1 # (--mixed, the default) undo it; keep changes, unstaged
git reset --hard HEAD~1 # undo it AND throw the changes away
git reset path/to/file # just unstage a file; nothing is lost
The three modes
| Mode | Branch moves | Staging area | Working files |
|---|---|---|---|
--soft | yes | untouched | untouched |
--mixed (default) | yes | reset | untouched |
--hard | yes | reset | overwritten: uncommitted work is lost |
Think of it as how far to unwind. --soft only moves the pointer, so your changes stay ready to commit again. --hard also discards them.
Typical uses
- Fix the last commit:
git reset --soft HEAD~1, adjust, commit again (like amend). - Unstage something you added by mistake.
- Throw away local experiments:
git reset --hard origin/mainto match the remote.
Cautions
--harddestroys uncommitted changes. There’s no undo for work Git never saved. Checkgit statusfirst, or stash.- Don’t reset commits you’ve pushed to a shared branch. Others have them, and you’d have to force push. Use
git revertto undo shared history. - Committed work isn’t gone immediately. The reflog lists where
HEADhas been, sogit reset --hard <hash>can bring a commit back.
Reset rewrites history, and revert adds to it. When in doubt, revert.