Open In App

What is Git Restore?

Last Updated : 31 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn about a very interesting git command called ‘git restore’. We will be discussing different use cases and real-life examples, and in the end, we will see one demo of ‘git restore’.

Use cases of git restore

  1. git restore help us ‘unstage’ or even discard ‘uncommitted’ local changes.
  2. It can be used to undo the effect of ‘git add’ and unstaged the changes you have previously added in the staging area.
  3. It can also be used to discard local ‘uncommitted’ changes in your project, thereby restoring the last committed state.

Real-Life Example

Let’s say your technical club organizes one hackathon, and today is the award ceremony for it. The announcers are just announcing the winners. So, the announcer, by mistake, calls “Team 30” the winners, and he calls that team on the stage.

Staging-files-USing-'git-add'

But the real winners were “Team 13” so the next task of the announcer would be to apologize to the team which was on stage and unstaged them(Team, 30).

Unstaging-files-using-git-restore

The next task here would be to call the correct winners on stage, so the announcer now announces the correct winners (Team 13) and calls all of them on stage, and then all the winners are awarded trophies. So, this was one example by which you can relate and understand the use of the ‘git restore’ command.

Let’s say there is one file and you make some changes to it, and then you add those changes to the staging area with the help of ‘git add’ but later you realize that you have added the wrong files on the staging area so, in this case, you can always unstaged the wrong changes you have done and then stage the changes that you want to do.

Example

Let’s start with an empty HTML file.

4.jpg

Empty file

Let’s make some changes to this file and then check the status

5.jpg

initial changes done

Let’s add this file to the staging area and again check the status of the file

6.jpg

added files to staging area

Let’s unstage this file using git restore

git restore --staged [file_name]

Let’s check the status again

7.jpg

checking status after unstaging

Let’s discard the uncommitted change

git restore [file_name]
8.jpg

discarding uncommitted changes

If we see the file now then it will be at its initial stage. Finally, let’s do some necessary changes and add this file to the staging area, and then commit them.

9.jpg

adding correct changes

So, this was the practical example of git status, we hope you have understood the concept of ‘git restore’.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads