patterngitMajor
git reset --soft vs --mixed vs --hard: know before you run
Viewed 0 times
git resetsoft resetmixed resethard resetuncommitunstage
Problem
Running the wrong
git reset mode discards work permanently. Many developers confuse the three modes and lose staged changes or working directory edits.Solution
Three modes, three scopes:
--soft: moves HEAD only. Index (staging) and working tree unchanged.
Use to: uncommit but keep all changes staged.
git reset --soft HEAD~1
--mixed: (default) moves HEAD + resets index. Working tree unchanged.
Use to: uncommit and unstage changes, keep files edited.
git reset HEAD~1
--hard: moves HEAD + resets index + resets working tree.
Use to: completely discard commits AND local edits.
git reset --hard HEAD~1 ← DESTRUCTIVE
--soft: moves HEAD only. Index (staging) and working tree unchanged.
Use to: uncommit but keep all changes staged.
git reset --soft HEAD~1
--mixed: (default) moves HEAD + resets index. Working tree unchanged.
Use to: uncommit and unstage changes, keep files edited.
git reset HEAD~1
--hard: moves HEAD + resets index + resets working tree.
Use to: completely discard commits AND local edits.
git reset --hard HEAD~1 ← DESTRUCTIVE
Why
Git maintains three trees: HEAD (last commit), Index (staging area), Working Tree (disk files). Each reset mode affects a different number of those trees.
Gotchas
- --hard permanently discards uncommitted working tree changes with no undo (unless you have an IDE local history)
- All three modes move branch pointer — use
git revertinstead if commit is already pushed to shared branch git reset <file>only unstages the file (equivalent to --mixed for a single path)
Code Snippets
The three reset modes with practical use cases
# Undo last commit, keep changes staged (ready to recommit)
git reset --soft HEAD~1
# Undo last commit, keep changes in working tree (unstaged)
git reset HEAD~1
# Completely discard last commit and all its changes
git reset --hard HEAD~1
# Unstage a single file without touching working tree
git reset HEAD path/to/file.jsContext
Undoing commits or unstaging changes locally before pushing
Revisions (0)
No revisions yet.