How Git Works Internally?
Tags: git, github
Published: 2023-10-30 15:59:21

---
title: How Git Works Internally?
published: true
date: 2023-10-30 15:36:45 UTC
tags: git, github
canonical_url: https://chandel.hashnode.dev/how-git-works-internally
---

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k3xuaziku6w33bx2xumq.png)


All of us use git on a daily basis. But how many of us know what goes on under the hood? In this blog post, we will take a deep dive into the inner workings of Git.

### **How does Git store data?**

Git is a content-addressable file system, which means its a key-value store where you can insert any type of content and get back a unique key that you can later use to retrieve that content.

| Key | Object |
|-----|--------|
| accefceba62b4874a613a2336de33ee716e99931 | `console.log(Hello World!);` |
| deb909034d4053be6bdc041e8280e6d0f6eecfe | `console.log(Foo Bar);` |

The key is generated from the content of the object, using the SHA-1 hash function. So, two files having the same content will have the same key🧐(more on this later). Also, git compresses the content of the object using the zlib library to save disk space.

If you have used git before, then you have probably seen the directory called `.git`.This is where git stores almost everything. The object database is stored in the `.git/objects` directory.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698675640645/33a894ec-8a6b-4992-b1ce-0561aeb104f3.png)

### **Git Objectsblob, tree, and commit**

In Git, the contents of files are stored in objects called **blobs** , binary large objects. These blobs are different from regular files because they don't keep any extra information like when they were created or their names. Each blob is identified by a unique SHA-1 hash. SHA-1 hashes consist of 20 bytes, often represented using 40 characters in hexadecimal form(In this blog, we may occasionally display only the initial characters of this hash).

Lets add a file to git and see, what a blob object and its hash look like.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698675702769/0558e5e8-2583-4595-bbe5-4d37882d3e29.png)

Here a new object is created with a key `accefceba62b4874a613a2336de33ee716e99931`(Note Git utilizes the initial two characters of the SHA-1 hash as a directory name and the remaining characters are used as the filename for the file that actually contains the blob. Git does this to reduce the number of files per directory).

We can check the type & content of the object by following commands:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698675744080/08e6e178-8bfd-4f33-b287-b24bde878c41.png)

Now let's create another file and add it to git.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698675858336/7813ff53-b116-48b1-a387-af57e336c090.png)

As you can see from the `main2.js` a new blob object is created with a different key since the content of the file is different. What if the content of 2 or more files has the same content?

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698676318618/da8eab5d-01f9-466a-8605-287bf24035b9.png)

Git does not create duplicate objects 😉.

In `git`, the equivalent of a directory is a **tree**. A tree is basically a directory listing, referring to blobs as well as other trees. Trees are identified by their SHA-1 hashes as well.

Lets take a snapshot of that file systemand store all the files that existed at that time, along with their contents.

In Git, a snapshot is represented as a **commit** , containing a reference to the main tree (the root directory) and additional metadata, including the committer, commit message, and commit timestamp. Typically, commits also have one or more parent commits, representing previous snapshots. These commit objects are also identified by SHA-1 hashes, which you often encounter when using `git log`.

Lets restructure our code a bit and make the first commit. Move the files `main2.js` `main3.js` to directory `src` and commit. Our directory and git objects will look something like this:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698676512074/c93257d0-4579-4d7d-abf3-b9535b837764.png)

Time to traverse this git graph of objects. We will start from the commit. Get the commit hash using the command `git log`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698676809015/93180f2f-57a1-41ef-af71-a7a44acfc3b5.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698676839229/4f4cde26-d508-4721-801e-61120ebe440b.png)

When we print a commit object in Git, it reveals essential commit details, including the author's information, the commit message, and the commit timestamp. Additionally, it provides a hash pointing to a tree object. If we delve into the tree object by printing it, we find the hash for the `main.js` blob and another tree object representing the `src` directory. Continuing this process, printing the src tree object uncovers the hashes for the remaining two blobs: `main2.js` and `main3.js`.

This is the fundamental way Git organizes and stores its data in objects. To simplify the concept, refer to the image below for a visual representation.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698676954580/0fdca759-8228-4098-bab5-b6cb913dc230.png)

### **Time for a second commit?**

What happens when we change the content of a file, and make a new commit? Before reading on further, pause for a while and think how git would store it.

Let's say we add an `!` at the end of the line in `main.js`, that is `console.log("Hello World");` is changed to `console.log("Hello World!");`.

Well, this change would mean a new blob will be created with a new SHA-1 hash.

Since we have a new hash, the tree no longer points to accef. This means the trees content is changed and so will its hash.

Almost ready to create a new commit object, and it seems like we are going to store a lot of data - the entire file system, once more! But that is not the case. Actually, most objects, specifically blob objects, havent changed since the previous commit.

So this is the trick - as long as an object doesnt change, we dont store it again. We only refer to them by their hash values. We can then create our commit object.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698677083327/908a1011-a17b-42b9-9bda-90afafb8c366.png)

Since this commit is not the first commit, it has a parent commit `bcc69`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698677108473/f9e226c8-537e-4f9c-a0f7-1ffe0e568cd3.png)

### **Summary**

In this blog post, we dived into the inner workings of Git, exploring its fundamental components: **blobs** , **trees** , and **commits**.

We learned that a **blob** holds the contents of a file. A **tree** is a directory-listing, containing **blobs** and/or sub- **trees**. A **commit** is a snapshot of our working directory, with some meta-data such as the time or the commit message. We refrained from delving into more concepts, such as branches and tags, to keep the post concise.

Thanks for reading!