Open In App

Git – Object Model

Last Updated : 30 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

When we work with git, the first command that we use to initialize our repository to git repository is the git init command. After using this command, we can see that the .git folder is created. Let’s get deep dive into the folder and see what all we have got in the .git folder.

We can see that we have many subdirectories in the .git folder, but the subdirectory that we would have a look at is the .git/objects subdirectory. Generally, the objects folder consists of four different types of objects – blob, tree, commit and tag

.git/objects directory is empty as of now but we will see some changes in it and we will understand why this directory is important to us. In Git, every commit, every tree, and every file is saved in the objects folder as a hash value. There is a unique hash value for every object which helps Git to where it is located. The folders are created accordingly. We will learn about hashes as we move forward. Since the objects folder is empty as of now, let’s create a file demo.txt and write “Hello Geeks” in it.

We are using Ubuntu here but Windows users can work with the git bash . Ubuntu Commands can be run on git bash.

Now, let’s add our changes to the staging area and commit our changes by using – 

git add demo.txt 

git commit -m “First Commit”

Let us observe changes in our objects folder after this command.

So we got new directories which are named 2 characters long and have 38 characters long file name.

Note: You might have different hashes in your computers so no need to worry if you don’t get the same hashes.

Git generates a 40-character checksum (SHA-1) hash for every object and the first two characters of that checksum are used as the directory name and the other 38 as a file name.

Therefore the 40 character hashes that we have are –

6d510f79378ca9954e1078a70ff4fdaaa64494fe

7c5d8f39237365acdf66527189ba4fbb6c59b4fc

893f819f4a3b12c76aec907ad636edff48ffcfad

Let us now check what are these 3 different objects and their types. We will use the command – git cat-file 

However , the command that we would be needing are – 

  • git cat-file -t <hash value>  // used for showing type of object
  • git cat-file -p <hash-value> // used for showing content of object

Let us check the type of our objects – 

git cat-file -t <40 character hash value>  //checks the type of the object

So, the types that we have got are – blob, tree, and commit. Let us understand them in more detail.

1. Blob Object

Blob – Binary Large Object

Blob stores the contents of the file. Whenever we commit our files the first type of objects that are created are the blob objects. Let us have a look at the content of our blob object with the help of –

git cat-file -p <blob object hash value> 

We can see that we got the content of our file demo.txt that we wrote initially i.e Hello Geeks.

2. Tree Object

Tree objects contain a list of all files in our repository with a pointer to the blob object assigned to them. Let us have a look at the content of our tree object with the help of –

git cat-file -p <tree object hash value> 

Each line in a Tree object has – file permissions, object type, object hash, and filename. Let us break down the line – 

File Permission (100644) – Permissions of 644 means that the owner of the file has read and write access, while the other users on the system only have read access. blob represents the type of object. 40 character hash represents the hash of the object. You can verify it with your blob hash. demo.txt represents the filename.

3. Commit Object

Git creates a commit object that has a pointer to its tree object. The commit object contains – tree object hash, parent commit hash, author, committer, date, and message. Let us have a look at the content of our commit object with the help of –

git log 

git cat-file -p <commit object hash value> 

As clear from the above image, the commit object consists of – Tree object, Name of the author, Name of the committer, and the commit message. Since we have covered 3 different types of objects, we are left with one i.e Tag Object.

4. Tag Object

A tag object contains an object name, object type, tag name, the name of the person who created the tag, and a message. Let us make an annotated tag using – 

git tag -a “first-tag” -m “Tag For First Commit” 

Let us now check our objects folder to see whether the tag object is created or not.

We can see that a new directory “97” is being added. Let us now verify our type and see the contents of our tag object.

git cat-file -t <tag object hash value>

git cat-file -p <tag object hash value>

We can see that the tag object is pointing to the commit object (verify using our commit hash value) as we expected. I hope you would have found this article useful. We have covered all four object types and you can try more by doing some changes and repeating above the steps again.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads