Undo changes
Your safety net
⏱ Est. ~8 min
01 · Read
Made a mistake? git can restore a file back to its last committed version. It's a safety net — as long as you've committed recently, you can always go back to that good state.
💡 Picture thisThink of git checkout -- as Ctrl+Z for an entire file. If you accidentally delete half of an essay, you can rescue it from the last photo (commit) you took. The catch: you can only restore what you've previously saved.
Key points
- git checkout -- filename restores a file to its last committed state
- This throws away all uncommitted changes to that file — permanently
- That's why committing often matters: more commits = more restore points
02 · Terminal exercise
Set up a git repo first, so we have a safety net to use.
(This section is interactive — please enable JavaScript.)
03 · Terminal exercise
Stage the file.
(This section is interactive — please enable JavaScript.)
04 · Terminal exercise
Commit this working version.
(This section is interactive — please enable JavaScript.)
05 · Quiz
Why is committing often a good habit?
- git requires at least one commit per hour
- More commits = more restore points when things go wrong
- Makes the repo smaller
- You must commit before using git diff
06 · Terminal exercise
Uh oh! You accidentally overwrote the entire file with garbage.
(This section is interactive — please enable JavaScript.)
07 · Terminal exercise
See the damage with your own eyes.
(This section is interactive — please enable JavaScript.)
08 · Terminal exercise
Use git checkout to restore the file to its last committed version. -- tells git you mean a file, not a branch.
(This section is interactive — please enable JavaScript.)
09 · Quiz
You accidentally deleted important code in app.js but haven't committed yet. What happens when you run git checkout -- app.js?
- It creates a new branch called app.js
- It permanently deletes app.js
- It restores app.js to its last committed version
- It shows a diff of what changed in app.js
Other lessons in this chapter
⚠ The full interactive experience needs JavaScript. Please enable it and reload.
※ This is an independent Traditional Chinese teaching project — not an official Anthropic product. Claude™ is a trademark of Anthropic, PBC.