Tracking and Staging in Git

Harsh Seksaria
Version Control System, Git and GitHub

--

Hey there. Hope you’re doing well.

In the last blog, we discussed setting up our folder to make it a Git repo and set basic configuration variables. In this blog, we will learn what staging and tracking mean.

Working tree: A working tree is a current state of the files that you are modifying currently. All the files edited in your project folder is said to be in working tree. The working tree contains all the files that Git is tracking, and any new files that we created and which Git is not tracking currently. In short, working tree contains current versions files or current state of files.

So, as we initialized git in last blog, right now your working tree is empty. Say, I create a new file, so it will be contained in folder and is untracked by Git. Git doesn't know that it has to keep an eye on that file. To make Git track your files, use the following command:

git add <file_name>

We can also add all the files at once using any of the following command:

git add .orgit add -A

This adds your file to the staging area. A staging area is a file maintained by the Git containing all the information about the files and changes that will go in your next commit.

Let’s see this in action.

First, I created a simple Python script in my folder.

Created the new file: hello.py

We use git status command to see the current state of our files.

git status output
Output of the command

As you can see in the above image, the git status command lists out a few things. But what are these?

First it shows which branch we are on(will be discussed later).

Then it shows that there's no commit yet in this repo.

After that, it lists out the untracked files, as we discussed earlier. Git knows there is a file but it isn't tracking it yet. So, we use git add . to tell Git to track it, which will move it to the staging area. Then we see the status again. Recall which command we just used.

git status after staging
Output of the command

Ooooooh! Something changed.😮

Now, Git tells us that there are some files staged and ready to be committed. Commits will be discussed in a later blog. After adding any file to staging area, it is ready for commit, but if you make any changes to it again, you again need to add it.

Now, let’s say you were working on a temporary file and added it by mistake; you didn't intend to include it in a commit. Or you add a sensitive file like one containing credentials to connect to a database, by mistake. You can do one of the two things:

1. Simply delete the file

2. If you want to keep working on it but not include it in a commit now, use the following command:

git rm --cached <file_name>
Complex
Read the paragraph below

You need to observe two things here. First, we have staged files; being tracked by Git. And we have untracked files, not yet added to staging area. Lets see what’s the status after I add the new file.

Staging new file
The new file is also staged and ready to be committed.

Let’s try removing it from the staging area using the command I mentioned before.

Removing from staged area
Status after removing

After removing, the file again goes to the list of untracked files.

Summary

To track files and put them in staging area:

git add <file_name>orgit add .orgit add -A

To see the status:

git status

To remove files from staging area:

git rm --cached <file_name>

Adding transfers files from untracked to staged; unstaging puts it back to the untracked list.

My name is Harsh Seksaria. I like to write about the topics simplifying them as much as possible for you to understand without unnecessary effort. Still, sometimes something may not be very clear. If you find any mistake, error, or you have a suggestion, just mail me. I’ll try to improve it asap.

My email: harsh20.freelance@gmail.com

My LinkedIn: www.linkedin.com/in/harshseksaria

--

--

Harsh Seksaria
Version Control System, Git and GitHub

MSc Data Science @ Christ (Deemed to be University), Bangalore, INDIA