Resetting Commits
23.1 What Resetting Commits Means
Resetting moves HEAD to an earlier commit. Depending on the reset type, Git may also modify the staging area and working directory. This is used only on local commits.
23.2 Soft Reset (--soft)
Moves HEAD backward but keeps changes staged.
Use case:
• Edit or combine recent commits
• Change commit message or grouping
Example:
1git reset --soft <commit>Use case:
• Edit or combine recent commits
• Change commit message or grouping
Example:
1git reset --soft HEAD~123.3 Mixed Reset (--mixed) - Default
Moves HEAD backward and unstages changes but keeps file modifications.
or explicitly:
Use case:
• Rework staged files
• Reorganize commits
1git reset <commit>or explicitly:
1git reset --mixed <commit>Use case:
• Rework staged files
• Reorganize commits
23.4 Hard Reset (--hard)
Moves HEAD backward and discards all changes.
Use case:
• Throw away commits and changes completely
⚠️ Irreversible if commits are not backed up.
1git reset --hard <commit>Use case:
• Throw away commits and changes completely
⚠️ Irreversible if commits are not backed up.
23.5 Resetting to a Specific Commit
Reset directly using commit hash:
Git history beyond that commit is removed from the current branch.
1git reset --hard abc1234Git history beyond that commit is removed from the current branch.
23.6 Reset vs Revert (Quick Contrast)
• Reset rewrites history (local only)
• Revert creates a new commit (safe for shared branches)
Never reset commits that have already been pushed to shared branches.
• Revert creates a new commit (safe for shared branches)
Never reset commits that have already been pushed to shared branches.
23.7 Safe Usage Rules
• Use reset only on local branches
• Avoid --hard unless certain
• Prefer revert for public history
• Backup commit hashes if unsure
• Avoid --hard unless certain
• Prefer revert for public history
• Backup commit hashes if unsure
23.8 Key Takeaways
• git reset moves HEAD backward.
• --soft keeps changes staged.
• --mixed keeps changes unstaged (default).
• --hard deletes commits and changes permanently.
• Never reset shared or pushed commits.
• --soft keeps changes staged.
• --mixed keeps changes unstaged (default).
• --hard deletes commits and changes permanently.
• Never reset shared or pushed commits.