How to use git restore command

Git restore command can be used for many undo operations. Git version 2.23.0 introduced git restore command, the current version of your git command can be checked using the command.

git --version

Unmodify a file

Git restore command can used to revert file changes. Suppose we have project repository with one file readme.

From the above git status output we can see readme file modified. To revert the file changes run the following command.

git restore <filename>

It is important to understand restore is a dangerous command. Any local changes you made to the file will be lost and git will replace the file with the last committed or staged version. If the local changes are not necessary then you can use this command. Older git versions can use git checkout <file> to revert file changes.

Unstage a file

In the project folder, added a new file contrib.md using git add * .

From the above status command output, we can see accidentally added contrib.md file. To remove contrib.md file from the staging area we need to run the following command.

git restore --staged <file>

After executing the above command the file will unstaged.

For git restore manual run the following command.

git restore --help
git restore -h

Leave a Comment