Choosing Between Soft, Mixed, and Hard Resets
When you want to undo a commit, Git gives you three levels of aggression:
- Soft (
git reset --soft HEAD~1): Moves the HEAD pointer back by one commit. All files that were in the commit stay staged in your index. Ideal when you want to re-commit with a different message or combine multiple commits. - Mixed (
git reset HEAD~1): The default mode. Undoes the commit and unstages the files, leaving them in your working folder as uncommitted modifications. - Hard (
git reset --hard HEAD~1): Completely discards the commit and wipes changes from your local disk. Use with extreme caution.
Frequently Asked Questions
How do I undo the last commit without losing my work?
Run 'git reset --soft HEAD~1'. This undoes the commit but leaves all modified files staged in your index, so none of your edits are lost.
What is the difference between git reset --soft and git reset HEAD~1?
'git reset --soft HEAD~1' keeps your changes staged (ready to commit). 'git reset HEAD~1' (mixed mode) keeps your changes unstaged in your working directory (open in your editor).
Can I just edit the last commit message instead of resetting?
Yes! If you haven't pushed yet, simply run 'git commit --amend -m "Your new commit message"'. If you missed a file, stage it with 'git add <file>' then run 'git commit --amend --no-edit'.
Will 'git reset --hard' delete untracked files?
'git reset --hard' wipes modified tracked files, but leaves untracked new files alone unless you also run 'git clean -fd'. Always create a stash backup before using --hard.