In this article we will see how to discard local changes and how to skip the staging area.

Git Discarding Local Changes & Skipping the Staging Area

We will learn 2 things in this article

  1. Discard local changes
  2. Skip staging area

So let’s see one by one.

Discard Local Changes:

Some time we might need to discard the changes and go back to the previous version of the code. To do that we will create a new file and commit.

$ cat>file_name 

Use the above command to create a file and add some text to it. After we are done, use ctrl+S to save it.

Git Discarding Local Changes & Skipping the Staging Area_One

Content in the file. (Opened new_file.txt)

Git Discarding Local Changes & Skipping the Staging Area_Two

Now commit the changes using

 $ git commit -am

Git Discarding Local Changes & Skipping the Staging Area_Three

Let’s remove and add some other lines to the txt file.

Git Discarding Local Changes & Skipping the Staging Area_Four

Now we can see the changes using

$ git status -s

We can see that there are some changes made to the file we created.

Now we want to restore this file to its previous version(where it was empty). To do so we need to use the command

$ git restore file_name
Git Discarding Local Changes & Skipping the Staging Area_Five

This should restore our file back. Now when we check the status again

$ git status -s

To confirm let’s see the text file contentsGit Discarding Local Changes & Skipping the Staging Area_Six

Skip staging area:

Staging area is the place where all modified files are shown. That is why it is not advisable to skip the staging area but if you are sure that there are no unnecessary files you can skip them.

Normally we would add the files to the staging area using

$ git add

Git Discarding Local Changes & Skipping the Staging Area_Seven

And then we commit the changes using

$ git commitGit Discarding Local Changes & Skipping the Staging Area_EightHowever to skip the staging area we can do it by directly committing

Use the command

$ git commit -a

This will push all the files

To add the message use the command

$ git commit -am “message” Git Discarding Local Changes & Skipping the Staging Area_Nine

Leave a Reply

Your email address will not be published. Required fields are marked *