What does git restore do, and how is it different from git checkout for files?
git restore discards changes in a file or unstages it. It was added in Git 2.23 to split the file-restoring job away from git checkout, which also switched branches and confused beginners.
Explain it simply
Old git checkout did too many jobs: change branches, create branches, and throw away file edits. Git 2.23 split them. git switch handles branches and git restore handles files.
git restore file replaces the working copy with the staged version, so your unstaged edits are gone for good. git restore --staged file only unstages and leaves your edits in place. git restore --source=HEAD~2 file pulls an older version of the file in.
Be careful: discarding unstaged edits cannot be undone, because Git never saved them.
Example
git restore app.js # throw away unstaged edits
git restore --staged app.js # unstage, keep the edits
git restore --source=HEAD~2 app.js # old version of one file
# old way: git checkout -- app.jsWhat to say in the interview
"git restore is the modern command for undoing file changes. Plain restore discards unstaged edits, --staged unstages without losing work, and --source brings back a file from another commit. It replaces the file-related uses of git checkout, which now only needs to be remembered for older Git versions."
Common mistakes
- Running git restore on a file with edits you wanted; unstaged work is not recoverable.