Oops, undo!
You broke a file and want yesterday's version back. Or you staged the wrong thing. Or your commit message has a typo. Each has a precise, safe fix.
The undo toolkit
git init echo "Welcome!" > index.html git add . git commit -m "Add page" echo "oops" > index.html git restore index.html cat index.html echo "secret" > notes.txt git add notes.txt git restore --staged notes.txt git status git add notes.txt git commit -m "Add nots" git commit --amend -m "Add notes" git log --oneline
- Discard edits to a file → git restore file
- Unstage a file → git restore --staged file
- Fix the last commit's message or add a forgotten file → git add … then git commit --amend