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
- Discard local changes
- 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.
Use the above command to create a file and add some text to it. After we are done, use ctrl+S to save it.
Content in the file. (Opened new_file.txt)
Now commit the changes using
$ git commit -am
Let’s remove and add some other lines to the txt file.
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
This should restore our file back. Now when we check the status again
$ git status -s
To confirm let’s see the text file contents
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
And then we commit the changes using
$ git commitHowever 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