The three reset modes
| Mode | Commit | Index (staging) | Working files |
|---|---|---|---|
--soft | Undone | Changes stay staged | Untouched |
--mixed (default) | Undone | Changes unstaged | Untouched |
--hard | Undone | Reset | Discarded |
git reset --soft HEAD~1 # undo commit, keep everything staged
git reset HEAD~1 # undo commit, unstage, keep files
git reset --hard HEAD~1 # undo commit and discard all changes
Which mode to choose
- You committed too early and want to keep editing:
--softor--mixed. - You want to split the commit into smaller ones:
--mixed, then stage in pieces. - You committed a broken experiment and want it gone:
--hard, after confirming nothing else depends on it. - You already pushed: do not reset; use
git revert.
Recovering from a hard reset
git reset --hard moves the branch, but the old commit still exists in the object database. git reflog records the previous HEAD position; reset back to it or create a branch there.
git reflog # find the commit you reset away
git reset --hard <sha> # go back
git branch rescue <sha> # or save it on a new branch
Uncommitted changes destroyed by --hard are not in the reflog. Commit or stash before destructive operations.
Undo a pushed commit
On a shared branch, rewriting history forces everyone else to recover manually. Use git revert <sha>, which adds an inverse commit and leaves history intact. git push --force-with-lease is only appropriate on your own branch, and only after understanding who else may have pulled it.