The Three Stages Of Git

Reginald LynchJanuary 14th 2021, 3:59

In this post, we'll see how to create a blob, tree, and commit object in the repository. We'll also learn about the three stages of creating a commit.

Step 1 : First, we'll make a small change to the file and check git status:

echo "Another line" >> text-file.txt
git status

Step 2 : Let's add the text-file.txt file and run git status again:

git add text-file.txt
git status

Step 3 : We can edit the file again and run git status:

echo "Another line 2" >> text-file.txt
git status

Step 4 : Let's also add the second change:

git add text-file.txt
git status

Step 5 : Now, all the changes we have made to the file are ready to be committed, and we can record a commit:

git commit -m 'Another change to text file'

Step 6 : The add command creates the blob, tree, and commit objects; however, they are also created when we run the commit command. We can view these objects using the cat-file command

git cat-file -p HEAD

Step 7 : The root-tree object from the commit is as follows:

git cat-file -p HEAD^{tree}

Step 8 : We can use the git fsck command to check for dangling objects—that is, objects that are not referred to by other objects or references

git fsck --dangling

Step 9 : Let's check the content of the blob using the following command:

git cat-file -p 07f9907ae7df0d7dbc14c424c490e6025c2e92e8

The blob was, as expected, similar to the content of text-file.txt when we added it to the staging area the first time.

The following diagram describes the tree stages and the commands used to move between the stages: