Discarding Uncommitted Changes
How to use `git restore` to safely unstage files or permanently discard local edits.
The git restore command
git restore is a command used to restore files in your working directory or staging area to a previous state.
Introduced in Git 2.23 (alongside git switch), it specifically handles modifying file contents. It replaces the older, confusing git checkout -- <file> command, giving developers a precise tool to undo local changes.
Why it matters
Web development involves rapid iteration—tweaking CSS, testing logic, breaking layouts. It is incredibly common to experiment with a file and realize you want to completely undo your work.
git restore gives you a scalpel to quickly discard those local edits, or to unstage a file you accidentally added, without touching your commit history.
How it works (Two Modes)
**Mode 1 (Discard):** Running git restore <file> copies the version of the file from the Staging Area and overwrites the Working Directory. Your local edits are destroyed.
**Mode 2 (Unstage):** Running git restore --staged <file> copies the version of the file from the Last Commit (HEAD) and overwrites the Staging Area. Crucially, your working directory edits remain completely intact.
Check yourself
Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.
Remember this
git restore <file>permanently discards unstaged local changes.git restore --staged <file>safely unstages a file without losing work.- Think of it as copying a file from the past to overwrite the present.
Done with this concept?
Mark it complete to track your progress. No login needed.