Understanding git reset
git reset moves the current branch reference and can also modify the staging area and working tree depending on its mode. --soft moves HEAD while keeping changes staged. --mixed, the default, moves HEAD and resets the index while keeping working-tree changes. --hard resets HEAD, the index, and working tree.
Reset is powerful and should be used carefully, particularly on shared branches.
Use --hard Carefully
git reset --hard can remove uncommitted tracked changes. Make sure important work is committed, stashed, or otherwise backed up before using it.
Reset Modes
git reset --soft HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1💡
--soft preserves staged changes, --mixed preserves them only in the working tree, and --hard discards tracked working-tree and staging changes that differ from the target.Key Takeaways
- Soft reset preserves changes in the index.
- Mixed reset is the default and unstages changes.
- Hard reset can discard local tracked changes.
Knowledge Check
Which reset mode keeps changes staged after moving HEAD?