How Git Stores Data. Blob, tree, and commits | by Marcin | May, 2022

Blob, tree, and commits

After I began utilizing Git, I did what most individuals do. I memorized instructions to get the job achieved with out actually understanding what was taking place below the hood. Generally, I used to be getting the outcomes I wished. However I used to be nonetheless pissed off that I used to be often ‘breaking’ the repo-getting it right into a state I didn’t count on and never understanding the right way to repair it.

Is your expertise comparable?

The shortcut method to utilizing a repository is an try to make use of a software with out doing the important homework to be taught the way it works. In my case, all the things ‘clicked’ as quickly as I learn in regards to the inside knowledge mannequin utilized by Git. You see, Git is a form of a database, and one would by no means have the ability to work with SQL, for instance, with out understanding what a desk, file, and so forth. is. Let’s cowl the information hole and see a little bit of the internals of a Git repository.

Git is a distributed model management software program, which suggests you don’t want an exterior server to make use of it. All the information that Git wants is saved within the .git folder. As a Git person, you haven’t any enterprise altering these information, however for the needs of this text, we’ll have a look inside to see how Git shops the information.

Simply after creating the repository with git init, you will discover inside:

$ ls -R .git
HEAD config description hooks information objects refs

applypatch-msg.pattern pre-applypatch.pattern pre-rebase.pattern replace.pattern
commit-msg.pattern pre-commit.pattern pre-receive.pattern
fsmonitor-watchman.pattern pre-merge-commit.pattern prepare-commit-msg.pattern
post-update.pattern pre-push.pattern push-to-checkout.pattern


information pack



heads tags



Proper now, it’s nearly empty: we now have just a few folders, principally instance information for hooks. We’ll ignore these; our focus on this article might be principally .git/objects content-the main knowledge storage in Git.

Git shops each single model of every file it tracks as a blob. Git identifies blobs by the hash of their content material and retains them in .git/objects. Any change to the file content material will generate a very new blob object.

The best approach to create an object is so as to add an object to the stage. What’s within the stage might be a part of the subsequent commit. Staging is the “pre-commit” state in git. It’s the place we hold information that aren’t already dedicated however already tracked by Git.

Let’s create a easy file and make a blob to characterize it:

$ echo "Take a look at" > check.txt

With this command, we write “Take a look at” to the check.txt file. To make it a blob, we simply want so as to add it to the stage by operating:

$ git add .

After including our new file to the stage, inside .git/objects, we now have:

$ ls -R .git/objects
34 information pack




We have now a brand new folder, 34, and inside that folder a file 5e6aef713208c8d50cdea23b85e6ad831f0449. It’s because the content material hash is 345e....: the 2 chars from the entrance are used as a listing. The content material of this file is:

$ cat .git/objects/34/5e6aef713208c8d50cdea23b85e6ad831f0449

It’s compressed for storage effectivity. We will see what’s inside by operating the next Git command:

$ git cat-file blob 345e6aef713208c8d50cdea23b85e6ad831f0449
Take a look at

We have now solely the content material inside-no metadata for the file.

Let’s see what occurs if we make some modifications to the file and add the up to date model:

$ echo "Take a look at 2" >> check.txt

This command provides a brand new line,”Take a look at 2″, to the present file check.txt.

Let’s add the present model to the stage:

$ git add .

And see what we now have contained in the .git/objects folder:

$ ls -R .git/objects
34 d2 information pack




Now we now have two objects, the second contained in the d2 subfolder. Its content material is:

$ git cat-file blob d277ba2806ce99d418b0b5d6c28643deca0e36dc
Take a look at
Take a look at 2

It’s the identical as our up to date textual content.txt:

$ cat check.txt
Take a look at
Take a look at 2

As we will see, Git shops the whole file for every model.

The tree objects are how Git is storing folders. They reference different issues as their content material:

  • information are added by their blob
  • subfolders are added by their tree

For every reference, a tree shops:

  • file or folder title
  • blob or tree hash
  • object sort
  • permissions

Like with blobs, Git identifies every tree by the hash of its content material. As a result of the tree is referencing the hash of every file it incorporates, any change to the content material of information will trigger the creation of a wholly new tree object.

Equally, as a result of completely different variations of the identical file can have a number of blobs, Git will create one other tree object for every folder model.

Normally, you create a tree as a part of the commit. We’ll cowl commits later on this article, however within the meantime, let’s use git write-tree-a plumbing command that creates a tree primarily based on what’s inside our staging.

Plumbing and porcelain instructions come from an analogy utilized in Git:

  • porcelain — user-friendly command meant for finish customers. Similar because the showerhead or faucet in your toilet.
  • plumbing — inside instructions wanted to make the porcelain work. Similar because the plumbing in your home.

Except you’re doing superior stuff, you don’t have to know plumbing instructions.

With our staging as earlier than, we run:

$ git write-tree

The returned worth is the ID of our new tree object. To look inside, you possibly can run:

$ git cat-file -p fd4f9947de2805e460bfeeca3346e3d36d617d37
100644 blob d277ba2806ce99d418b0b5d6c28643deca0e36dc check.txt

Regardless that it’s a distinct knowledge sort than blobs, their worth is saved in the identical place:

$ ls -R .git/objects
34 d2 fd information pack




All the information is in the identical folder construction.

Now, we’ll add one other folder inside to see how nested bushes are saved:

# create a brand new folder
$ mkdir nested #
# add a file & it’s content material
$ echo 'lorem' > nested/ipsum
# including it to the stage
$ git add .

Making a tree now will give us a brand new ID:

$ git write-tree

Its content material:

$ git ls-tree 25517090ae5d0eb08f694de6d38d613615fe99e4
040000 tree bc9a36d27aa303a3b1cab543b64c6944fea5ce8b nested
100644 blob d277ba2806ce99d418b0b5d6c28643deca0e36dc check.txt

We will see that nested was added as a tree reference. Let’s examine what’s inside:

$ git ls-tree bc9a36d27aa303a3b1cab543b64c6944fea5ce8b
100644 blob 3e9ffe066cd7b2ce4c6fb5c8f858496194e1c251 ipsum

As you possibly can see, it’s one other tree object that describes a folder’s content material. With many tree objects, you possibly can describe any nested folder construction.

A commit is a whole description of the state of the repository. It incorporates the next data:

  • reference for the tree object that describes the topmost folder
  • commit creator, committer, and time
  • father or mother commit(s)-commits that we primarily based this commit on

Most commits have just one father or mother, with the next exceptions:

  • first commit in historical past has no dad and mom
  • merge commits have multiple

As earlier than, Git identifies every commit by the hash of its content material. Due to this fact, any change to the information, folder, or commit metadata will create a brand new commit.

We will create our first commit with the usual commit command:

$ git commit -m 'first commit'
[main (root-commit) 26349a2] first commit
2 information modified, 3 insertions(+)
create mode 100644 nested/ipsum
create mode 100644 check.txt

The output reveals the truncated commit ID. Let’s discover a full worth:

$ git present
commit 26349a25253f9b316db1a5d3c3f23c1ca5ca4e0e (HEAD -> important)
Creator: Marcin Wosinek <>
Date: Thu Apr 28 18:18:07 2022 +0200

first commit

To see the content material of commit object, we will use:

$ git cat-file -p 26349a25253f9b316db1a5d3c3f23c1ca5ca4e0e
tree 25517090ae5d0eb08f694de6d38d613615fe99e4
creator Marcin Wosinek <> 1651162687 +0200
committer Marcin Wosinek <> 1651162687 +0200

first commit

The tree reference is similar as what we had within the earlier instance. We will see that commits keep in the identical folder as different objects:

$ ls -R .git/objects
25 26 34 3e bc d2 fd information pack


Let’s restore the primary model of our check.txt file:

$ echo "Take a look at" > check.txt

This command overwrites the present file with “Take a look at”.

$ git add .

Provides the up to date model to the staging.

$ git commit -m 'second commit'
[main 7f54a43] second commit
1 file modified, 1 deletion(-)

Commits modifications.

Let’s discover the total ID:

$ git present
commit 7f54a437d87cd1f241cfb893c4823bc7e60c19ec (HEAD -> important)
Creator: Marcin Wosinek <>
Date: Thu Apr 28 18:37:55 2022 +0200

second commit

The commit content material is thus:

$ git cat-file -p 7f54a437d87cd1f241cfb893c4823bc7e60c19ec
tree 04b0192c1c88ac1c1a96f386e84e5388ef8a509a
father or mother 26349a25253f9b316db1a5d3c3f23c1ca5ca4e0e
creator Marcin Wosinek <> 1651163875 +0200
committer Marcin Wosinek <> 1651163875 +0200

second commit

Git has added the father or mother line as a result of we commit on prime of one other commit.

Different necessary knowledge saved by Git are simply references to a most up-to-date commit. So my important department is retailer in .git/refs/heads/important, and its content material is

$ cat .git/refs/heads/important

or the ID of its topmost commit. We will discover all of the related data from the ever-expanding tree of commits:

  • department historical past as instructed by commit messages
  • who made a change and when it was made
  • the connection between completely different branches and tags

After I create a easy tag:

A file is created in .git/refs/tags:

$ cat .git/refs/tags/v1

As you possibly can see, each tags and branches are express references to a commit. The one distinction between them is how Git treats them once we create a brand new commit:

  • present department is moved to the brand new commit
  • tags are left unchanged

The blob, tree, and commits are how Git shops the whole historical past of your repository. It does all of the references by the item hash: there is no such thing as a approach of manipulating the historical past or information tracked within the repository with out breaking the relations.

Did you discover this text useful? Subscribe to be notified once I publish new articles on programming and JavaScript.

More Posts