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
Navigate to the directory of the repository (First Project).
Type the following command
git statusand Press enter to execute the command.
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:
Note: The touch command is the easiest way to create new, empty files.
Press enter to create the file.
Once the file is created, execute the
git statuscommand again.
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.
- Add the file to the staging area.
After you have added the file, again look at the
git status command and what does it say this time.
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)
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
Note: echo command is to used to add text in the text file.
- 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.
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.
- Add the file to the staging area by
git addcommand and then type
git statusagain to see the status.
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.
- To delete the file (ABC.txt), type the following command and Press enter to remove the file.
Note: RM stands for remove.
- After removing the file, again check the
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.