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>.

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.


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


git restore --staged <file-name>