git restore command

The git restore is used to undo the changes. Suppose we have made some changes to a file since our last commit. We have saved the file but then realize we definitely do NOT want those changes anymore! To restore the file to the contents in the HEAD (or restore the file version of the last commit), use git restore <file-name>.

Syntax git restore <file-name> The above command is same as git checkout HEAD <file-name>

Unmodifying Files with source option git restore <file-name> restores using HEAD as the default source, but we can change that using the --source option.

For example, git restore --source HEAD~1 home.html will restore the contents of home.html to its state from the commit prior to HEAD. We can also use a particular commit hash as the source.

Bash

git restore --source HEAD~1 app.js

Unstaging Files with Restore
If we have accidentally added a file to our staging area with git add and we don't wish to include it in the next commit, we can use git restore to remove it from staging.

Use the --staged option like this: git restore --staged app.js

Bash

git restore --staged <file-name>
Quiz Time!

Choose the best response for each question and then select Submit Quiz.

Q1: What is the primary purpose of the git restore command?

Q2: Which command will restore a file to its state in the last commit?

Q3: What is the equivalent command to git restore <file-name>?

Q4: How can you change the source from which a file is restored using git restore?

Q5: What will the command git restore --source HEAD~1 home.html do?

Q6: If you have accidentally added a file to the staging area and want to remove it, which command should you use?

Q7: Which option is used with git restore to remove a file from the staging area?

Q8: What does the command git restore --staged app.js do?