About git reset command helper
git reset moves your branch pointer to another commit. The three modes differ in what happens to your changes.
Modes
- --soft: moves the branch, keeps changes staged.
- --mixed (default): moves the branch, keeps changes in your working files but unstaged.
- --hard: moves the branch and discards all uncommitted changes. This cannot be undone easily.
Recovering
If you reset by mistake, git reflog lists recent positions of HEAD, so you can often return with git reset --hard <hash>.
A worked example
To undo the last commit but keep your work staged, run git reset --soft HEAD~1. To unstage files and keep the changes in your folder, run git reset HEAD~1 (mixed). To throw everything away and match the remote, git reset --hard origin/main, which cannot be undone for uncommitted changes.
Reset versus revert
On a shared branch that others have pulled, use git revert, which adds a new commit that undoes an old one. Reset rewrites history and would force everyone else to fix their copies.
