How to use git restore effectively
Tags: git, beginners, tutorial, productivity
Published: 2020-09-26 13:27:36

---
title: How to use git restore effectively
published: true
tags: git, beginners, tutorial, productivity
canonical_url: https://gitbetter.substack.com/p/how-to-use-git-restore-effectively
---
![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F40dfcf90-e67e-4e7a-9114-bb85d109e801_1000x420.png)

> #### This post was originally posted in the newsletter [GitBetter](https://gitbetter.substack.com/). If you are interested in leveling up your game in Git, you can subscribe to it.


**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
```
![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F791d7c0e-4c58-463c-8165-1baaeb5c3f04_603x370.png)

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>
```
![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F806b41c4-437e-43a5-9dbd-c10838394808_641x601.png)

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
```
![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9d16a55d-760a-4529-bc01-209df1f639dc_522x219.png)

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.

Thank you for reading :)

> #### This post was originally posted in the newsletter [GitBetter](https://gitbetter.substack.com/). If you are interested in leveling up your game in Git, you can subscribe to it.