Undo, carefully
Three safety nets, in order of safety. Nothing here is a time machine.
Know exactly what each undo discards: restore throws away edits, restore --staged only unstages, amend rewrites the last commit.
01Undo an unstaged edit
You changed a file and it is wrong — you want the last saved version back, on disk, right now. git restore <file> does exactly that: it throws away the edit and puts back the version from your last commit or staging. The file is instantly as if you never touched it.
Read that second sentence twice before you ever type it: the edit is gone forever. Restore is not a toggle — it is a shredder. That is why this course exists: the three undo commands differ in exactly how much they destroy, and typing the wrong one is the classic beginner disaster.
git restore notes.txtUnstaged edits are the only copy of that work — no photograph holds them yet. git restore deletes them permanently. If the edit might matter, cat the file and copy anything you want to keep into another file first.
02Undo a staging
Staged a file by mistake? git restore --staged <file> takes it off the checklist — and this is the gentle undo: the file keeps its changes on disk, untouched. It simply returns to waiting, as if you never staged it. Compare the two: restore discards your edits; restore --staged discards only the staging decision. The flag is the entire difference.
A tiny mnemonic that sticks: --staged has the word staged in it, and that is the only thing it touches.
| command | what it discards | what survives |
|---|---|---|
| git restore file | your unstaged edits | nothing of the edit |
| git restore --staged file | the staging decision | all edits, on disk |
| git commit --amend | nothing — rewrites | see below |
git restore --staged notes.txt03Fix the last commit
You committed and immediately noticed: a typo in the message, or a file you forgot to stage. git commit --amend rewrites the last commit — whatever is staged now gets folded in, and the message is replaced. It is an edit of history: no new commit appears; the newest one is quietly replaced by a corrected twin.
Amend is made for the first minute after committing — the oh-wait moment. Anything staged when you amend joins the photograph; nothing staged and just -m, and only the message changes.
git commit --amend -m "fix the message"Amending is safe while the commit lives only on your machine. Once pushed, rewriting it confuses every other copy — leave pushed commits alone. Amend before push, never after.
Practice
A saved file and a messy situation. Untangle it with the three tools.
01.Break the file on purpose, then undo the edit with git restore.
hint: echo "broken" > notes.txt then git restore notes.txt
02.The last commit message is a typo (frist). Amend it to exactly: first save
hint: Try: git commit --amend -m "first save"