"I messed up a file and want the clean version"
A real file, genuinely corrupted. Restore it to the last commit — or to any earlier one you choose.
A stray paste, a bad find-and-replace, an accidental overwrite — salsa.md now genuinely contains nonsense instead of a recipe. Nothing has been committed yet, which means the clean version is still sitting safely in the last commit, one git restore away.\n\nThis lesson also answers a related, real question: what if you want to look at how a file used to read several commits ago — not necessarily to keep that old version, just to compare? git restore --source genuinely pulls any historical version into the working directory on demand, and restoring back to HEAD afterward is just as easy.
Restoring to the Last Commit
git restore salsa.md
The working directory copy is genuinely overwritten with whatever the last commit has for that file — every bit of the corruption is gone, replaced with the real, clean version.
Restoring a Specific Earlier Version, Just to Look
git log --oneline --no-decorate -- cookies.md
git show 22779d0:cookies.md
git restore --source=22779d0 -- cookies.md
This genuinely overwrites cookies.md in the working directory with exactly how it looked at that one earlier commit — useful for comparing an old approach against the current one, without creating any commit yet.
Going Back to the Latest Version
git restore --source=HEAD -- cookies.md
Since nothing from the old version was staged or committed, this simply restores the current, real HEAD version — the comparison is over, nothing was actually changed.
git restore <file>
Overwrites a tracked file in the working directory with its last committed (HEAD) version. Confirmed directly in this lab: genuinely erases whatever corruption exists in the working copy, replacing it entirely with the real committed content.
git restore --source=<commit> -- <file>
Overwrites a file in the working directory with its version from any specific commit, not just HEAD. Confirmed directly in this lab pulling a real, several-commits-old version of cookies.md for comparison, then restoring back to HEAD once done.
💥 Corrupt a Real File
Overwrite salsa.md with genuine nonsense — exactly the kind of accident this lesson recovers from.
cd ~/recipes && printf 'asdkfjalksdjf garbage nonsense\n' > salsa.mdcd ~/recipes && cat salsa.mdcd ~/recipes && git statusstudent@lab:~$ cd ~/recipes && printf 'asdkfjalksdjf garbage nonsense\n' > salsa.md student@lab:~$ cd ~/recipes && cat salsa.md asdkfjalksdjf garbage nonsense student@lab:~$ cd ~/recipes && git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: salsa.md Untracked files: (use "git add <file>..." to include in what will be committed) notes.txt no changes added to commit (use "git add" and/or "git commit -a")
🧹 Restore the Clean Version
Use git restore to overwrite the corrupted salsa.md with its last committed version.
cd ~/recipes && git restore salsa.mdcd ~/recipes && cat salsa.mdstudent@lab:~$ cd ~/recipes && git restore salsa.md student@lab:~$ cd ~/recipes && cat salsa.md Fresh Salsa Dice tomatoes, onion, cilantro; mix with lime juice and salt.
🕰️ Pull In a Specific Earlier Version to Compare
Find an old version of cookies.md several commits back, and pull it into the working directory just to look at it.
cd ~/recipes && git log --oneline --no-decorate -- cookies.mdcd ~/recipes && git show 22779d0:cookies.mdcd ~/recipes && git restore --source=22779d0 -- cookies.mdcd ~/recipes && cat cookies.mdstudent@lab:~$ cd ~/recipes && git log --oneline --no-decorate -- cookies.md 5997d5c Add serving note to cookie recipe 7451729 Increase butter back to 175g for a richer flavor 59cee07 Reduce butter to 150g per taste-test feedback 22779d0 Add butter quantity to cookie ingredients 9db68e6 Add variations note to cookie recipe dda949a Credit the recipe source in the cookie title c7a2617 Add step-by-step instructions to cookie recipe fd509ba asdf c764cd9 Add prep time estimates to all three recipes cd7e65b Add ingredients list and pasta recipe 7fb33b3 Add chocolate chip cookie recipe student@lab:~$ cd ~/recipes && git show 22779d0:cookies.md Chocolate Chip Cookie Recipe (Grandma's) Ingredients: flour, sugar, butter (200g), chocolate chips Serves: 24 cookies Prep time: 15 minutes Instructions: 1. Cream the fat and sugar together. 2. Beat in the eggs one at a time. 3. Mix in the flour and chocolate chips. 4. Drop spoonfuls onto a baking sheet. 5. Bake at 190C for 10-12 minutes. Variations: add walnuts, or swap half the chocolate chips for M&Ms. student@lab:~$ cd ~/recipes && git restore --source=22779d0 -- cookies.md student@lab:~$ cd ~/recipes && cat cookies.md Chocolate Chip Cookie Recipe (Grandma's) Ingredients: flour, sugar, butter (200g), chocolate chips Serves: 24 cookies Prep time: 15 minutes Instructions: 1. Cream the fat and sugar together. 2. Beat in the eggs one at a time. 3. Mix in the flour and chocolate chips. 4. Drop spoonfuls onto a baking sheet. 5. Bake at 190C for 10-12 minutes. Variations: add walnuts, or swap half the chocolate chips for M&Ms.
↩️ Restore Back to the Latest Version
The comparison is done — bring cookies.md back to its current, real HEAD version.
cd ~/recipes && git restore --source=HEAD -- cookies.mdcd ~/recipes && cat cookies.mdcd ~/recipes && git statusstudent@lab:~$ cd ~/recipes && git restore --source=HEAD -- cookies.md student@lab:~$ cd ~/recipes && cat cookies.md Chocolate Chip Cookie Recipe (Grandma's) Ingredients: flour, sugar, butter (175g), chocolate chips Serves: 24 cookies Prep time: 15 minutes Serve warm, ideally straight off the baking sheet. Instructions: 1. Cream the fat and sugar together. 2. Beat in the eggs one at a time. 3. Mix in the flour and chocolate chips. 4. Drop spoonfuls onto a baking sheet. 5. Bake at 190C for 10-12 minutes. Variations: add walnuts, or swap half the chocolate chips for M&Ms. student@lab:~$ cd ~/recipes && git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) notes.txt nothing added to commit but untracked files present (use "git add" to track)
Lab 1.4.3 complete. A genuinely corrupted file recovered, and any historical version made available on demand:\n\n\n File genuinely corrupted : ✅ real mistake\n Restored to last commit : ✅ git restore\n Earlier version pulled in : ✅ git restore --source\n Restored back to HEAD : ✅ comparison complete\n
Enable JavaScript to run the live terminal and track your progress.