Git & GitHub
/Intermediate
Restore Files (Legacy)
Definition
Using the legacy `git checkout -- <file>` command to discard changes in the working directory and restore the file to a previous state.
Explain Like I'm New
Before `git restore` was invented in 2019, developers used `git checkout` to discard file changes. It is heavily used in older tutorials and by senior developers due to muscle memory.
Real World Example
Typing `git checkout -- index.html` to instantly erase any unsaved changes you made to the HTML file.
Common Use Cases
- •Discarding changes in older Git versions
Terminal Output
bash / terminal
# DANGER: This permanently deletes any unsaved changes to app.js
$ git checkout -- app.js
# DANGER: Discard all unsaved changes in the entire project
$ git checkout -- .
# Note: While this works perfectly, modern best practice is to use:
# $ git restore app.js
Interview Questions
basic
- What does the double dash (`--`) do in `git checkout -- file.txt`?
intermediate
- Why did the Git team create `git restore` to replace this?