Sitemap
Towards Dev

A publication for sharing projects, ideas, codes, and new theories.

Mastering Git: Git Objects

5 min readFeb 12, 2024

--

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.

Press enter or click to view image in full size

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:

  1. header
  2. content

Header

The header is made up of three elements:

  1. the identifier blob (or tree)
  2. size of the content in bytes
  3. \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.

Press enter or click to view image in full size

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 --stdin

output:

7108f7ecb345ee9d0084193f147cdad4d2998293

git hash-object would 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 --stdin option tells git hash-object to 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?' | shasum

output:

7108f7ecb345ee9d0084193f147cdad4d2998293

The hashes are the same!

Press enter or click to view image in full size

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:

Press enter or click to view image in full size

In more detail:

Press enter or click to view image in full size

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.

Press enter or click to view image in full size

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:

Press enter or click to view image in full size

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! :)

--

--

Amirhosein Gharaati
Amirhosein Gharaati

Written by Amirhosein Gharaati

Software Engineer, love to share knowledge

Towards Dev
Towards Dev

Published in Towards Dev

A publication for sharing projects, ideas, codes, and new theories.