Enroll in Selenium Training

In the last tutorial about committing the changes inside Git, we learned that first, we need to take the file to the staging area and then commit from there. Working on different files in the same repository all at once can make you lose track of the changes you did. You might forget that you modified something or maybe you added a new file which is of no use now. You need something which can help you keep track of it. In Git, this work is done by the command called git status. In this tutorial, we will focus on the git status command.

Git Status Command

Git status command is used in Git to know the status of the working tree. It shows the state of your working directory and helps you see all the files which are untracked by Git, staged, or unstaged. In shorter terms, Git will show you any difference in the current tree and the HEAD pointer (Refer Git Terminologies). Along with this Git status will also show you the changed or new file in the repository. We will explore each of these ones through examples. We will cover the following cases of using Git Status

  • On a newly created file
  • On a modified file
  • On a deleted file

Git Status when working tree is Clean

Before we make any new changes let's see the status of the Git repository in which we are working (First Project). Refer Git Repository

1.Open Git Bash

  1. Navigate to the directory of the repository (First Project).

  2. Type the following command git status and Press enter to execute the command.

Git Status Command in Git

As clearly visible, there is nothing to commit and the working tree is clean i.e. there are no untracked files.

Git Status when a new file is Created

Now let's make some changes and see what happens.

1.Create a file ABC.txt this using command:

touch ABC.txt

touch_ABC

NoteThe touch command is the easiest way to create new, empty files.

  1. Press enter to create the file.

  2. Once the file is created, execute the git status command again.

git_status_untracked_file

The message is quite clear that there is nothing to commit but untracked files are present. Untracked files as discussed in the Git commit tutorial is the file that is not yet added to the staging area. There are two types of untracked files in Git. One is the normal project file that we saw above and the other is the binary files that are inside the repository like .obj or .exe.

Git in the above image also provides suggestion so as to use git add to track the file. These suggestions will be provided according to the situation by the status command. You will get different suggestions in different cases as we will see throughout this tutorial.

  1. Add the file to the staging area.

git_add_ABCtxt

After you have added the file, again look at the git status command and what does it say this time.

git_status_new_file

new file: ABC.txt: This tells you that a new file is present (with its name) and also been added to staging.

This comes very handily when you have to commit, before committing blindly. Looking at the git status can help a lot to avoid the changes that we don't want to commit at all.

5. Commit this file. (Git Commit tutorial)

git_commit_new_file

After committing the file successfully, you can once again check the git status and it will show you that there is nothing to commit.

Now we will see what happens when an existing file is modified.

Git Status when an existing file is Modified

In this section, we will see the git status response when executed on a file that has been recently modified.

1.Type one sentence in the file ABC.txt

echo_ABC_file

Note: echo command is to used to add text in the text file.

  1. Press enter to write this sentence into your file ABC.txt. Once done, execute the git status command once again to see what it says now.

git_status_modified

modified: ABC.txt : This says that the file ABC.txt has been modified. It is in red because we have not yet added it to the staging area and this is the reason that the bottom line says that there are no changes added to commit.

  1. Add the file to the staging area by git add command and then type git status again to see the status.

git_status_modified_staged

modified : ABC.txt:  This message is turned to Green from Red. Also, the message about nothing to commit has disappeared. This means that we have our file inside the staging area, it is being tracked and there are changes to be committed. Commit the file to commit the changes we just made.

Git Status when the file is Deleted

Now that we have seen the git status command with a newly created file and modified file, let us see how it would respond to a deleted file.

  1. To delete the file (ABC.txt), type the following command and Press enter to remove the file.

rm <file_name>

rm_command

Note: RM stands for remove.

  1. After removing the file, again check the git status command.

git_status_deleted_file

deleted: ABC.txt: Message says the file named ABC.txt is Deleted.

You can commit the changes as we did in the above sections. It is very important to practice the git status command before executing any commit. Since you might not want any unwanted commits to happen. Also, it will give you information about the latest changes that have happened. Talking about committing the changes, we will move on to our next tutorial where we will learn about committing the changes with good commit messages.

Alter Files in Git
Alter Files in Git
Previous Article
Harish Rajora
I am a computer science engineer. I love to keep growing as the technological world grows. I feel there is no powerful tool than a computer to change the world in any way. Apart from my field of study, I like reading books a lot and developing new stuff.
Reviewers
Lakshay Sharma's Photo
Lakshay Sharma

Similar Articles

Feedback