learn git.
0/25 done
learn git/remotes and beyond/course 24 of 25

Undo, carefully

Three safety nets, in order of safety. Nothing here is a time machine.

8 min read·3 sections·2 tasks
the key idea

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.

[bash]· back to the last saved version
git restore notes.txt
restore means discard

Unstaged 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.

commandwhat it discardswhat survives
git restore fileyour unstaged editsnothing of the edit
git restore --staged filethe staging decisionall edits, on disk
git commit --amendnothing — rewritessee below
[bash]· off the checklist, edits intact
git restore --staged notes.txt

03Fix 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.

[bash]· the last commit, but better
git commit --amend -m "fix the message"
The one rule of amend

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.

[practice]· a real terminal, safely simulated
practice terminal — everything here is simulated, nothing can break
type 'help' to see the commands this terminal understands
~/my-project (main) $
Your tasks0/2 done
  1. 01.Break the file on purpose, then undo the edit with git restore.

    hint: echo "broken" > notes.txt then git restore notes.txt

  2. 02.The last commit message is a typo (frist). Amend it to exactly: first save

    hint: Try: git commit --amend -m "first save"