git reset changes your current branch’s HEAD. It can move commits, adjust the index, and rewrite the working tree. The command runs in three primary modes:
- Soft Reset (
git reset --soft <commit>): Moves HEAD to a commit but keeps changes staged. - Mixed Reset (
git reset --mixed <commit>): Moves HEAD and unstages changes, leaving them in your working directory. - Hard Reset (
git reset --hard <commit>): Moves HEAD and resets index and working directory to the specified commit, discarding all changes.
Precision comes from selecting the correct mode for the situation and the correct commit reference. Blind resets risk data loss, especially with --hard. Use git log --oneline or git reflog to confirm the exact commit before resetting.
Safe Practices for Git Reset
- Verify commit hashes before executing.
- Use
--softor--mixedwhen unsure. - Back up with
git stashfor temporary safety. - Avoid resetting shared branches unless coordinated.
Advanced Precision Techniques
For targeted reset behavior, combine git reset with partial paths: git reset <commit> -- path/to/file resets only the specified file to the state at that commit. This avoids resetting unrelated code changes.