Git Reset Soft vs Mixed vs Hard: Key Differences and Usage
git reset command has three main modes: soft moves the HEAD pointer but keeps changes staged, mixed (default) moves HEAD and unstages changes but keeps them in the working directory, and hard resets HEAD, staging area, and working directory to a specific commit, discarding all changes.Quick Comparison
This table summarizes how git reset modes affect the commit history, staging area, and working directory.
| Aspect | Soft Reset | Mixed Reset | Hard Reset |
|---|---|---|---|
| Moves HEAD pointer | Yes | Yes | Yes |
| Changes staged (index) | No | Yes (unstages changes) | Yes (resets index) |
| Changes in working directory | No | No | Yes (discards changes) |
| Effect on commit history | Moves back commits | Moves back commits | Moves back commits |
| Use case | Keep changes staged | Keep changes but unstage | Discard all changes |
Key Differences
git reset --soft moves the HEAD pointer to a specified commit but leaves the staging area and working directory unchanged. This means your changes remain staged and ready to commit again.
git reset --mixed (the default mode) moves HEAD and resets the staging area to match the commit, but it keeps your working directory changes intact. This unstages any changes but does not delete them, so you can edit or stage them again.
git reset --hard moves HEAD, resets the staging area, and also resets the working directory to match the commit. This discards all uncommitted changes, so use it carefully as it deletes local modifications.
Code Comparison
Here is how to use git reset --soft to move HEAD back one commit while keeping changes staged:
git reset --soft HEAD~1Mixed Reset Equivalent
Using git reset --mixed to move HEAD back one commit and unstage changes but keep them in the working directory:
git reset --mixed HEAD~1When to Use Which
Choose --soft when you want to undo a commit but keep your changes staged for a new commit.
Choose --mixed when you want to undo a commit and unstage changes but keep them available to edit or restage.
Choose --hard when you want to completely discard all changes and reset your project to a clean state at a specific commit.
Key Takeaways
git reset --soft moves HEAD but keeps changes staged.git reset --mixed moves HEAD and unstages changes but keeps them in the working directory.git reset --hard resets HEAD, staging area, and working directory, discarding all changes.--soft to redo commits without losing staged changes.--hard carefully as it deletes uncommitted work.