CodeBlanks Logo Icon CodeBlanks

git restore

Learn the modern git restore command.

What is git restore?

git restore is the modern Git command for discarding unwanted file changes. When used as git restore file.txt, it restores the specified working tree file to its state in the latest commit. This discards the file's uncommitted edits.

git restore notes.txt

This command operates only on the specified file.

Discard working tree changes

By default, git restore restores files in the working tree; it does not change staged content. For example, after editing a file, using git restore brings back the content from the latest commit.

git status
git restore app.js
git status

This operation can permanently discard the changes being restored. Check the current file content before restoring it.

Practice Use git restore

Discard the uncommitted working tree changes in style.css. Then check the result using git status.

Starter code

terminal
# Çalışma alanındaki değişikliği geri al

# Durumu kontrol et

Sample solution

terminal
git restore style.css
git status

Expected output

On branch main
nothing to commit, working tree clean

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does git restore do by default?

  • Automatically stages the file
  • Restores the file to its latest committed state Correct answer
  • Creates a new commit
  • Deletes the entire repository

Explanation: By default, <code>git restore</code> restores a working tree file to the content stored in HEAD.

2 Modern restoration command

Question
git ____ 
Answer
git restore 

Explanation: <code>git restore</code> is the modern command for discarding working tree file changes.

3 Restore concepts

Term Meaning
git restore Restores a file
Working tree Working area
HEAD Latest commit position
app.js File to restore

All lessons in Beginner