Free cookie consent management tool by TermsFeed Generator how to use git restore and completely ignore the changes: 3 scenarios (video) | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
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

https://youtu.be/gv6qSY0Hv1s

 

Transcription

Hello

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

great

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

Practice

Let’s practice together

Question

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

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.

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

git branch workflow: 7 easy steps (video)
Nov 27, 2022 programming

git branch workflow: 7 easy steps (video)

Transcription Hello This is amir Today we are going to talk about git branching workflow I will go through it step by step and help you understand the process ...

9 Min Read Read More
PHP Pass Arguments by Reference
Jan 12, 2023 programming

PHP Pass Arguments by Reference

Today we are going to talk about splat passing arguments by reference in PHP. By default when we pass argument to a function we pass it by value. ...

6 Min Read Read More
A Programmer's Guide to Debugging: Essential Steps to Follow
Mar 23, 2024 programming

A Programmer's Guide to Debugging: Essential Steps to Follow

Debugging is a crucial skill for any programmer, as it helps identify and fix issues in the code. Effective debugging not only improves the overall quality of your software but can also save you time and frustration. ...

10 Min Read Read More
How to Combine Excel Files Using Python
May 08, 2024 programming

How to Combine Excel Files Using Python

Sometimes you might have many excel files that you need to combine, whether it's reports, user data or anything else, merging them into one file with Python can be easily done. ...

9 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...