Mastering Git: Git Objects
There are multiple concepts about Git internals like Objects, References, Packfiles, and more.
In Git, objects are fundamental entities that represent different types of data in the repository. For example blobs, trees, commits . . .
To understand the Git better what exactly happens behind the scenes, we will explore git objects in this article.
Blob Objects
Git is a content-addressable file system.
At the core of Git is a simple key-value data store. What this means is that you can insert any kind of content into a Git repository, for which Git will hand you back a unique key you can use later to retrieve that content.
So we can say it is like a key-value store which:
- the key is the hash of the data
- the value is the data
And we can use the key to retrieve the data.
The Key
As most of you noticed, the key is the SHA-1 hash — a checksum of the content you’re storing plus a header (which you’ll learn about in a bit).
SHA1 Is a cryptographic hash function. If you give a piece of data, it produces a 40-digit hexadecimal number.
And as may you know, the value should always be the same if the given input is the same.
The Value
This object type that git is storing in the repository is called a blob.
Let’s simplify the structure. We separate the blob value into two groups:
- header
- content
Header
The header is made up of three elements:
- the identifier blob (or tree)
- size of the content in bytes
- \0 delimiter
Content
It is the data that you are storing in a file like a “what is up, doc?” text.
Git compresses the content with Zlib.
Let’s see it in action!
If you pass the text above to the hash-object command in git, you will see a hash output.
echo "what is up, doc?" | git hash-object --stdinoutput:
7108f7ecb345ee9d0084193f147cdad4d2998293
git hash-objectwould take the content you handed to it and merely return the unique key that would be used to store it in your Git database.the
--stdinoption tellsgit hash-objectto get the content to be processed from stdin; otherwise, the command would expect a filename argument at the end of the command containing the content to be used.
Now let’s examine the structure and see if the hash will be the same or not.
Get Amirhosein Gharaati’s stories in your inbox
Join Medium for free to get updates from this writer.
I will use the shasum command to directly generate the hash for the content I will pass to it:
echo -e 'blob 17\0what is up, doc?' | shasumoutput:
7108f7ecb345ee9d0084193f147cdad4d2998293The hashes are the same!
The blobs are stored in the “.git/objects directory (the object database)”. If you use tree .git command you will see the folder structure:
.git/objects directory (the object database)This object type is called a blob.
The first two characters are considered as a folder, and the rest are the characters of the hash value.
You can see the content by using this command:
git cat-file -p <hash-value>
Tree Objects
There is a key point here that the blob does not have some information like the filename or directory structure.
The next type of Git object is the tree which solves the problem of storing the filename and also allows you to store a group of files together.
Git stores content like a UNIX filesystem, but a bit simplified.
All the content is stored as tree and blob objects, with trees corresponding to UNIX directory entries and blobs corresponding more or less to inodes or file contents.
A tree contains pointers to blobs or other trees, and it has some metadata like the type of the pointer, filename, mode(executable or symbolink, …).
Following the previous repository, let’s create a directory and copy the file in it:
The main tree object in git will be something like this:
In more detail:
Notice that the “hello-copy.txt” hash value is the same with as the “hello.txt” file. Why? Because the content are same.
Identical content is stored only once!
If we examine the tree of the “test” directory, we see a blob that was stored before.
So if you have multiple files and folders in your git repository, the structure will be something like this: trees point to blobs and other trees.
Notice that no leaf is a tree! This is because git tracks blob objects rather than tree objects. This is the reason that you can’t store empty directories in the repository.
Commit Objects
So git stores different snapshots of your project that you want to track.
There is a big problem here: you must remember all SHA-1 values to recall the snapshots.
You also don’t have any information about who saved the snapshots, when they were saved, or why they were saved.
This is the basic information that the commit object stores for you. A commit points to a tree and contains: author, parent commits, author and committer, and commit message:
And the commits can not change!
If you change any data about the commit, the commit will have a new SHA1 hash. Even if the files don’t change, the created date will.
You can see more information about git objects in the main documentation:
If you liked my article, please give me feedback to improve and write more! :)











