Today I am here to tell you about “Git Three Tree Architecture“. It is a simple three step concept and easy to learn or do. These are said to be key concepts. There are three trees in this structure. I shall explain you their working and relation. We call them trees because they represents our file structure.
What We do? We pick the file from the repository tree, make changes in it inside working tree and then commit those changes inside the staging index tree. If you remember We did a commit in the last lecture. We not only did the commit we also added a file first.
GIT Three Tree Architecture
GIT three tree architecture is a very nice feature, because we can make changes to ten different files in our working tree. The interesting thing, if I don’t wanted to commit all the changed files. I just wanted to commit only five of those as a set. So I shall add those five files to the staging index and them commit it. Still I have having five files in my working tree.
In GIT three tree architecture, if we want to make change in the file which is present inside the repository tree. We don’t need to pull them first in the staging tree or then to the working tree, We pull them directly into the working tree.
So it is a simple pattern, We make changes in the working tree. Push these changed files in the staging index tree and push them with the commit message in the repository tree. This is the basic concept of GIT three tree architecture.
As you can see in the above picture the structure of three trees are defined. First is the working tree in which we will make change, then add the file in the staging index tree by “git add .” command. Then commit the file in to the repository tree.
GIT can work with multiple files at a time. Let Me explain this with the help of GIT three tree architecture. When we add a file in our project in the local machine. Then we give a command of “git add .” when we push the file in the staging index tree. Now the file exist in the staging index as well. The dot tell the git to gather all the changes that have been made till yet and add them in the staging index. Or you can also send a single file by writing it’s name.
Once we got every file in the staging index, We only need to commit it to push them inside repository tree by the help of “git commit“. Now this file is present with a commit change with it. By doing this in GIT three tree architecture My same file in present inside every tree with same name.
We have our file in all three trees so the trees look same. Now we will make changes inside the file in the working tree. It will give us the version 2 of the same file. Now we have to stage the changes inside the staging tree in GIT three tree architecture. We have all the required changes now, so we only need to commit them into the repository tree. Now version 2 of the file is present in all the trees. Same way we shall make another change for version three. So this is a simple GIT workflow that we will follow in GIT three tree architecture.
If we wanted to see these changes we will write git log and it will show all the changes which have made till yet.
Hash Values (SHA-1)
In GIT three tree architecture, when we commit the file they gain a specific ID which is different from all the other files. When file is added to the repository git generates a checksum for each change. This checksum algorithm convert data into a simple number. If the data changes so the checksum changes . Git uses SHA-1 hash algorithm. The generated number has 40 characters hexadecimal string(0-9)(a-f).
We can track our values by the help of this specific ID. Let me explain you this by picture.
In the above picture you can see that there are three yellow colored lines. These lines represents commits. As you can see they are the combination of 1-9 and a-f, the hexadecimal numbers. These are the specific ID’s assigned to each of the commit.
GIT three tree architecture deals with this reference variable called HEAD(all letters capital). Why we call it a pointer? It is because it points or refers to a specific commit. We will talk about the repository tree only because all the commits are made in there. As we make new commit, the pointer will change to the new commit.
HEAD always points to the tip of the current branch. Or you can say it the last state of our repository. You can say that the HEAD points to the parent of the next commit.
Let Me make it clear for you by the help of images. Take a closer look.
In the above picture you can see I made a commit and wrote a message in it. I displayed the change in one file. Then I checked the commit or the file by giving “git log” command. As you can see it is showing an ID in yellow color. The hexadecimal number. After that it showed the author name and the date of this commit. In the end the message I wrote.
Most of the people get started with the git without knowing about HEAD. I will explain this by the help of pictures.
The above picture shows the list of the git file. The hidden files inside it. As you can see there is a file with the name “HEAD“. This is the file which points to the parent file. If we wanted to see what is inside the head file, we need to give a new command. Take a closer look in the below picture.
AS you can see in the above picture I wrote a command “cat head”, which showed me the reference to where the header points. Now let’s see what inside the “refs” file. The below picture will show you.
As you can see inside the list there are four files. The third one is the head file. As defined by this statement “refs/heads/master“. There is a head file inside it and what else it shows that there is a file named master inside the heads file. Let’s check this out.
See this image, when I gave the command it listed the files inside the heads for me. The master file do exists inside it. Now let’s see what is inside this master file.
Now what do we have here? This is the ID on which the pointer is pointing. The hexadecimal code as shown above. This is the parent or most recent changed file ID.
Write the command properly other wise it will con work. Do mention the parent directory otherwise it will fail to work. Always fallow the sequences. Follow the pattern.
What we learned today? We learned the purpose of three tree architecture. The structure of GIT three tree and it’s functions. We learned the workflow of GIT. We also learned the purpose and working of HEAD pointer. We learned about the Hash value system, how it generates the ID for each commit.