Fixing mistakes: restore, reset, revert, stash
The tool depends on where the mistake lives:
- Not committed:
git restore <file>— discard the working change;git restore --staged <file>— unstage - Committed locally (not pushed):
git reset --soft HEAD~1— undo the commit, keep the changes;git commit --amend— fix the last commit - Pushed:
git revert <hash>— a NEW commit with the inverse change (history intact) - Setting work aside temporarily:
git stash→ latergit stash pop
The golden rule: never rewrite pushed history (reset + force push) — it breaks others' work. On shared branches, always revert. Also: reset --hard deletes changes IRREVERSIBLY — check git status first and be sure.
📚 Sources and documentation
- git-revertofficialgit-scm.com
- git-resetofficialgit-scm.com
- git-stashofficialgit-scm.com