how to use git restore and completely ignore the changes: 3 scenarios (video)
Last Updated on Nov 27, 2022
This is amir
Today we are going to talk about ways to ignore the edits we made in a git project and restore the changes by git restore
So let’s get started
Types of changes
There are 2 main types of edits we make in a project
- The first one is creating a file
- And the second one is updating or deleting a file
In the first scenario that we have created a file, the file is going to be shown as untracked.
Here I create a file named 1.txt and as you can see it’s untracked.
If you want to ignore it, you don’t need to do anything inside git you can simply delete the file
And when you run
it doesn’t show that file
Changed (git restore)
Now let’s talk about the second scenario when you changed a file
Here I’m creating a file named 1.txt again
Let me add and commit the changes.
Now let’s change it and write hello world
Now if we run
It it show as changed
If you want to ignore this changes
Then as it’s written here
Just run the command
and then the name of the file which in this case is 1.txt
Now you might ask what if I have already added the file but haven’t committed yet
Then for both of the scenarios, whether you’ve created a file or updated or deleted a file, it’s going to be listed as staged
So in this case you have to unstage it first.
And to do that You should run the command
and then the file name
When you unstage it Then it shows the file like before as changed or added
Let’s practice together
Here I initialize a git project I create a file named a.txt and commit the changes
Now I add a new file called b.txt
I also write “my name is amir” in a.txt
And I add them all but I haven’t committed yet.
Now I have changed my mind and I want to ignore all the changes and go back to before when there was only one file called a.txt and there was nothing written in it.
How can I do it?
Pause the video
Try to do it
Write your answer in the comment and then play the video to see the answer
Now let’s see the answer
As you can see all the files are in the staged area so I should unstage them first by running the command
Then for a.txt that is shown as changed
I should restore it by running the command
And for b.txt that is shown as untracked I can simply remove it
Was your answer correct? Write in the comments
Just a quick recap:
- We talked about ignoring the changes in a git project
- If we created a file we simply remove it
- If we changed or deleted a file we can restore it
- And if it’s shown as stages we should first unstage it and then ignore it
if you liked what you learned please like and subscribe
That would be a great motivation to do more videos
If you have any suggestions or questions please leave a comment below and I try to answer them as soon as possible
Have a wonderful day.
Link to Video:
You can watch the original video on youtube.