Resetting History: git reset
git reset --soft, git reset --mixed, git reset --hard, HEAD~, reset vs revert, dangerous commands
Resetting History: git reset
Git reset moves the current branch pointer backward to an earlier commit, effectively discarding everything after that point from the branch history. It is powerful but potentially destructive โ use it only on local, unpushed commits.
The Three Modes of Reset
--soft: Moves the branch pointer back but keeps all changes staged. Your work is preserved and ready to be re-committed.
git reset --soft HEAD~1--mixed (default): Moves the branch pointer back and unstages changes, but keeps them in the working directory.
git reset HEAD~1--hard: Moves the branch pointer back and discards all changes in both the staging area and working directory. This cannot be easily undone.
git reset --hard HEAD~1Practical Use Cases
Use --soft to combine several commits into one by resetting back and re-committing everything together. Use --mixed to unstage files without losing your changes. Use --hard with extreme caution โ only when you truly want to discard all recent work.
Recovering from Hard Reset
Even after a hard reset, commits are not immediately deleted. Git keeps them for 30 days in the reflog:
git reflog
git reset --hard abc1234Find the lost commit's hash in the reflog and reset to it.
