Git

Git Status

1. Introduction

In this example, we are going to discuss Git Status. The Git status command shows the state of the working directory and the staging area. It allows you to see staged changes and the files that aren’t being tracked. The technology we will use here is Git bash. Open Git bash on your PC and let’s begin.

2. Git Status with working tree clean

The git status command is run from a newly cloned repository in GitHub using the git clone command followed by the link to the repository you want to clone which can be copied directly on your github website. NB: You don’t need to add any argument after the git status command. Once you clone the repository and type the git status command, it triggers the following output.

Fig. 1. Status with working tree clean
Fig. 1. Status with working tree clean

The output released here can be interpreted as follows:

  • On branch master: Tells you on which branch you are found.
  • Your branch is up to date with ‘origin/master’: Meaning our local branch and remote branch commit history are synchronized.
  • Working tree clean: There are no changes in the working directory proving that the working directory has the latest commit.

3. Git Status when a new file is created:

Assuming we create a new file inside our cloned repository named style2.css. We can use the touch command to create a new file in git. Next, we need to check the status of our repository using the git status command which will output the following:

Fig. 2. Status when a new file is created
Fig. 2. Status when a new file is created

The output here shows "nothing is added to commit but untracked files present" (use "git add" to track). According to the output, it is suggesting we do a git add in order to track the new changes made. When we track the file using the git add command as said and retype the git status command, we get a different output as shown:

Fig. 3. Status when the new file created is added
Fig. 3. Status when the new file created is added

From the above output, we can see that the status after our file has been staged by outputting "Changes to be committed". It is important here to check the status every time before committing in order to avoid committing the changes we don’t want to commit. Let’s do a commit now and see the result as shown:

Fig. 4. Status after a new file created is committed
Fig. 4. Status after a new file created is committed

It is seen here that the current status after committing has been modified as clean again meaning that the remote repository has tracked the changes made on our local repository.

4. Git Status when an existing file is modified

In order to modify a file we use the "echo" command. The command will add some text to the specified file. Next, we need to check the status of our repository with the git status command as shown:

Fig. 5. Status when an existing file is modified
Fig. 5. Status when an existing file is modified

This command will add some text as shown to the specified file which in our case is the index.html file. Next, we can check the status of the repository by using the git status command as shown in the screenshot above. The output above shows us that the updated file is untracked. The output in red shows us that it is not yet staged once it will be, it will change from red to green as shown:

Fig. 6. Status when a modified existing file is added
Fig. 6. Status when a modified existing file is added

5. Git Status when a file is deleted

Assuming we want to delete the file we had created before named style2.css from our repository. In order to delete an existing file, we use the: $git rm <File Name> command. This command will delete a specific file from our local repository. After it is done, we now use the git status command as such:

Fig. 7. Status when a file is deleted
Fig. 7. Status when a file is deleted

From the output above, the status of the repository has been modified as deleted.

6. Conclusion

In addition to describing the status of your local branch, git status can also describe that of your remote branch relative to the local one. In order to get an accurate result we have to use the git fetch command first to ensure that our local repository is aware of the latest changes made on the remote repository. When collaborating with others on a repository in git, it is important to pay attention to the status of your repository. This information is useful to keep track of the local commits that you can push or those that need to be pulled.

Amanye Sirri

Sirri holds an Engineering Diploma in Computer Science with a major in Software Engineering from the African Institute of Computer Science in Cameroon. During her studies, she has been involved with a large number of projects ranging from programming to software engineering and design and analysis. She works as a computer teacher where she teaches students how to code in Java.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button