Open In App

Git – Working Tree

Improve
Improve
Like Article
Like
Save
Share
Report

Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git relies on the basis of distributed development of software where more than one developer may have access to the source code of a specific application and can modify changes to it that may be seen by other developers. Initially designed and developed by Linus Torvalds for Linux kernel development in 2005.

When we work with Git we mainly deal with the files and folders but one should not just relate Git with this, but Git provides us just more than that with Git Working Tree being one of them. 

Working Tree

When we have our project files ready locally, we initialize the project directory with git init command to make it a local git repository.  After initializing our directory with the git init command we can see .git folder which gets added to our directory. All the files and folders that we add to the Git repository residing outside the .git folder are known as the Git working tree. However, the .git folder is not a part of the working tree. This working tree tracks the files, folders, and the changes that we make inside them.

For example, If we modify the content of our file, the working tree takes a note of it and we get the information of it through the git status command. Similarly, we can inspect the files that are being edited and not added to the staging area. 

The repositories on Github, however, don’t have a working tree. However, on the local repository, we can get a good overview of the working tree. The command that helps us to get insight into the mechanics of the working tree is as follows:

git status

Let us look at an example, we will create an empty directory and initialize it to a git repository using git init command.

As discussed above the .git folder is not part of the working tree, the files that we would be adding to the directory now will become part of Working Tree.

Let’s add a new file demo.txt to our directory and then check our working tree using the command git status.

We can see there is some message given to us by the git status command, which states that there is a file called demo.txt which is there in our working tree which is currently being untracked.

Let’s add our file to the staging area and let’s observe the git status command.

Our file is now added to the staging area but we again got a message saying that we have not added commit. Let’s add commit using git commit command and check our working tree status.

Now we got a message that there is nothing to commit and the working tree is clean. Let’s modify our demo.txt and then again observe our working tree. 

As soon as we modified our demo.txt our working tree informs us that there is a change in the working directory. Let’s add these changes to the staging area and commit these changes.

So again, our working tree now becomes clean. Now, let’s observe what happens if we delete our file, will the working tree show those changes?

And we can see that after deleting our file, we get the message that there is a change that is untracked. So, let’s add those changes to the staging area and commit them.

So again our working tree becomes clean, let us look at the commits that we have had till now. So, in a similar manner, one can try on more examples to get a better understanding of the working tree.


Last Updated : 16 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads