0
0
GitComparisonBeginner · 3 min read

Git Reset Soft vs Mixed vs Hard: Key Differences and Usage

The 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.

AspectSoft ResetMixed ResetHard Reset
Moves HEAD pointerYesYesYes
Changes staged (index)NoYes (unstages changes)Yes (resets index)
Changes in working directoryNoNoYes (discards changes)
Effect on commit historyMoves back commitsMoves back commitsMoves back commits
Use caseKeep changes stagedKeep changes but unstageDiscard 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:

bash
git reset --soft HEAD~1
Output
HEAD is now at <commit-hash> Previous commit message Changes remain staged for commit.
↔️

Mixed Reset Equivalent

Using git reset --mixed to move HEAD back one commit and unstage changes but keep them in the working directory:

bash
git reset --mixed HEAD~1
Output
HEAD is now at <commit-hash> Previous commit message Changes unstaged but present in working directory.
🎯

When 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.
Use --soft to redo commits without losing staged changes.
Use --hard carefully as it deletes uncommitted work.