Git & GitHub Course
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?

Flash Cards

Question

What is the double dash?

Click to reveal answer
Answer

It explicitly tells Git: 'What follows is a file name, not a branch name'. This prevents a catastrophic error if you happen to have a file and a branch with the exact same name.

Question

Why replace?

Click to reveal answer
Answer

Because `git checkout` was too confusing. One command was used to safely switch branches (`git checkout dev`) AND destructively delete code (`git checkout -- file.txt`). Now, `switch` handles branches, and `restore` handles files.