how to use git restore and completely ignore the changes: 3 scenarios (video)

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

  1. The first one is creating a file
  2. And the second one is updating or deleting a file

Untracked (remove)

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

git status

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.

git add .

and then

git commit -m "changes added"

Now let’s change it and write hello world

Ok great

Now if we run

git status

It it show as changed

If you want to ignore this changes

Then as it’s written here

Just run the command

Git restore file-name

and then the name of the file which in this case is 1.txt

Git restore 1.txt

Staged (unstage)

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

Git restore --staged file-name

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

Git restore --stage .

Then for a.txt that is shown as changed

I should restore it by running the command

git restore a.txt

And for b.txt that is shown as untracked I can simply remove it

Was your answer correct? Write in the comments

Key Takeaways

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

Final Words

And finally

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

That’s it

Have a wonderful day.

Link to Video:

You can watch the original video on youtube.

Category: programming

Tags: #git

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts