What is Git Restore?
Last Updated :
31 Jul, 2023
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
- git restore help us ‘unstage’ or even discard ‘uncommitted’ local changes.
- It can be used to undo the effect of ‘git add’ and unstaged the changes you have previously added in the staging area.
- 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.
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).
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.
Empty file
Let’s make some changes to this file and then check the status
initial changes done
Let’s add this file to the staging area and again check the status of the file
added files to staging area
Let’s unstage this file using git restore
git restore --staged [file_name]
Let’s check the status again
checking status after unstaging
Let’s discard the uncommitted change
git restore [file_name]
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.
adding correct changes
So, this was the practical example of git status, we hope you have understood the concept of ‘git restore’.
Share your thoughts in the comments
Please Login to comment...