Git keeps a history, so most mistakes can be fixed. Check the state with git status before running any command that rewrites history.
Before committing
- git restore --staged file.txt: unstage a file, keep the changes
- git restore file.txt: discard unsaved changes in a file
- git stash: set changes aside for now
After committing
- git commit --amend: change the last commit message or add files to it
- git revert hash: create a new commit that undoes an earlier one, safe for shared branches
- git reset --soft HEAD~1: undo the last commit and keep the changes staged
Caution
- git reset --hard deletes uncommitted changes
- Do not rewrite commits that others have already pulled