How to use git restore effectively

git restore help you to restore uncommitted local changes

Hey there! I’m Srebalaji. You are receiving this email because you have subscribed to level up your game in Git.

git restore is used to restore or discard the uncommitted local changes of files.

Assume that you have done some changes in some files and then if you want to discard those local changes you can safely use git restore.

Another use case is if you want to unstage a file you can use this command. In other words, it is used to undo the effects of git add.

Usage

git restore <file_name>

Example:
git restore example.txt

In the above image, you can see that we have used git restore to discard the local changes of a file.

Other options

git restore one.txt two.txt // Mention multiple files

git restore . // Discard all local changes

git restore *.rb // Wildcard option

Unstage files

As said earlier, you can also use this command to unstage a file from the staging area.

git restore --staged <file_name>

You can see that the mentioned file is unstaged but the local changes of the files are still present.

Source option

git restore --source <source_id/commit_id>

The source option is used to restore a file from a particular commit. By default, restore will take the contents from HEAD

git restore --source f9456ba one.txt

In the above command, we are restoring a file from a mentioned commit.

Patch option

git restore one.txt -p

As you can see, you can select the chunks interactively.

git restore is one of the useful commands you can start using it in your workflow. It is marked as experimental in git docs but it’s not a very destructive command that you have to worry about.

That’s it for today :)

See you next week :) :)