Restoring to the Last Commit
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.
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.
This 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.
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.
Takeaways
- git restore <file> overwrites the working directory copy with the last committed (HEAD) version
- git restore --source=<commit> -- <file> pulls in any specific historical version, not just the most recent one
- Neither command creates a commit — they only ever change the working directory, always safely reversible with another restore
- Comparing an old version against the current one costs nothing — no commit, no branch, no risk