How do git commits actually work?
Tags: github, gitops, git
Published: 2023-02-21 15:48:06

---
title: How do git commits actually work?
published: true
date: 2023-02-10 11:16:17 UTC
tags: github,GitOps,git
canonical_url: https://www.aviator.co/blog/how-do-git-commits-actually-work/?utm_source=rss&utm_medium=rss&utm_campaign=how-do-git-commits-actually-work
---

![](https://github.githubassets.com/images/modules/site/social-cards/git-guides/git-commit.png)

Git is often one of the first tools developers learn to use, but it also is a black box of ~magic~ to many for that same reason. However, by learning about how Git operates at a lower level, you’ll more intuitively understand what is happening when you run commands like and `git cherry pick`.

Whether you’re a Git novice or an experienced developer, this post will provide valuable insights into the inner workings of the world’s most popular and powerful version control system.

## **[Git is a content-addressable filesystem](https://git-scm.com/book/en/v2/Git-Internals-Git-Objects)**

In other words, at the heart of Git lies a straightforward key-value data store. And, as its name suggests, it stores and accesses values (the data/ content) with keys (the content address).   
  
So what exactly do those keys and values look like? Hashes of the values, mostly.  
  
More specifically, the keys are just SHA-1 hashes of the values. So then what are the values / the content of which we are taking the hash? To understand that, we have to learn more about Git objects

## **Objects**

Git objects are the fundamental building blocks of a Git repository, used to store data such as commits, files, and directories. 

#### **Blob objects**

A blob, short for “binary large object,” is a type of Git object that stores the contents of a file as a snapshot in the Git object database.

To generate the hash for a blob object, Git calculates the SHA-1 hash of the file contents. This hash is later used to locate the blob object in the Git object database (.git/objects). When you make changes to a file and commit the changes, Git stores the new contents of the file ( **_in its entirety, not just the diff_** ) as a new blob object with a new SHA-1 hash. This allows Git to track the changes to the file over time and manage the file’s history.  
  
If there existed a JSON representation of a blob object, it could look something like this:

![](https://lh3.googleusercontent.com/6Rwyw66BrMBLdv0WIgI8ti0MsM1Q5f2PLDbxgeWtb70c-BmjfeGUP_HMURHvnDC0pYXc8d2WXSvqWmzipM3t7SkbReI47468ze7LKVC4TGvLkS0R7PSOTpVCF3nJzZ8R4f4fYmZnXVYS-zuquaf4bi8)

A couple things to note here First, git objects are not stored as JSON in Git. Second blob objects do not store the name of the file in it and only stores the contents of a file.

So then, how do we match a file with its respective contents? 

With tree objects! 

#### **Tree objects**

A tree object is a type of Git object that represents the contents of a directory in a Git repository. It’s an abstraction that enables Git to store a collection of files and directories together in a single object. 

It does this by including a list of the files and directories contained in the directory, along with the SHA-1 object name (a unique identifier) of each file or directory. This creates a mapping between the names of the files and directories to their corresponding hashes. 

To calculate the SHA-1 hash of a tree object, Git follows these steps:

Git creates a list of all the files and directories contained in the tree, along with their mode, type, and SHA-1 object name.

![](https://lh6.googleusercontent.com/lW7Hg0ss8LnxAS_nirx3o-XszD4TcLl_jkz2ffdZHs3ZQYpdIIbi-7_AXnSfxaONSN2CvgGBYP8I07clnw9ENzeVFyiTddlqKkMKL3jiHmWPwtBpT9t1q-rxj_z3w-JNhwvpVDTXvZPEaKMv0gdCBDE)

Git then sorts the list of files and directories alphabetically by name and creates a tree object that looks something like this: 

![](https://lh4.googleusercontent.com/43kNCIHrhg5YVP6yQjf23ufgvYYEaKUIezDucN3UEbXw0ZnPv32vgqzasAjj9Ac4h-WkWafrop63GvtkqJLJmMuRk4c0cqxtU3npXxBD86JltT65_ApM5HS8PsmgfEy_p3U_vNq5VKPAuI2Z5u82rQQ)

In Git, the file mode specifies the type and permissions of a file. The number 100644 indicates that the file is a normal, non-executable file. The blob keyword indicates that the following hash refers to a blob object. The hash abcdef01 is the SHA-1 object name of the blob object for the file file1.txt.

For each file or directory in the list, it retrieves the SHA-1 object name of the file or directory. Then, it concatenates the object names and file or directory names into a single string. For example:

![](https://lh3.googleusercontent.com/QlFHdcz25q2ujgxolRTKyrMi9hDxeLkY_6C-z31n-r7eMn9MCN1Yf9VP2jSriDNcA-DSXCO9wPUgpqCUhMf6OcotH0aV2GKeBJaRX7n7IGYvs7lP7425cH7sGZlQvhMsz9XQ5YXYnZurMzoaSkLOdhU)

This hash, **9a42e3cecbace1112a02720e43451f89266a261b** , would be the hash of the tree object.

This is just a general high-level overview of how Git creates an SHA-1 hash of a tree object.  
  
A visual representation of a JSON tree object might look something like this:

![](https://lh5.googleusercontent.com/a7UbmlZ96CaJT6gCmhaUA7RBrc_UgJl2IHWB4OHEVh-jHnrLJrMjN13NDEsUBgk5HV2W8lNmpY257RAs3N_ky8FOo9tAHS1sYkT_R0DWNvAZ1zZEdbrTUceycyZ33s_E5ffTAgz81Yc3XJgfHRxtwdo)

#### **Commit objects**

A commit object is a type of Git object that represents a snapshot of the repository at a particular point in time. It stores a reference to a tree object, which represents the state of the repository’s directories and their respective files at the time of the commit, as well as a reference to one or more parent commit objects. Commit objects also include metadata such as the commit message, author, and timestamp.

Commit objects are created when you run the git commit command, and they form a linear chain of commits in the repository’s history. Each commit object is linked to its parent(s) through a reference, creating a linked list of commits.

If there existed a JSON representation of a commit object, it would look something like the following: 

![](https://lh5.googleusercontent.com/k5GKHz6yqAxQXnWBtIBWtCLRe6kcAADadtB5mcvzVqOj2ArOBH9xDMPaRhGZeZQ6PWAK7_lm2-mKpdutpKNGh-7B_9wcIg1IjYtaAyN-zy0NbS-koooWQR3KVy70dknAgV380zHoJCm8Coxcz1BwaXU)

**Example**   
Now that we’ve learned about Git objects, let’s take a look at an example to actually see the inner workings of git using some plumbing commands! 

To follow along, set up a demo-repo and a file called test.txt with “test content” inside. Let’s also go ahead and add that to our staging area, which will generate a blob object for the file, **test.txt.**

![](https://lh6.googleusercontent.com/sPV7r6nEFjQbxliLjnjY-mb_em1rhIX5WNYohkVH1N4qQicuVRENzxr6VqatRMCa1KSfTL7oaxTvN_rXdRQSNMU1z8ycBoWUZ2PNsQfoRI847xUrY1cuMBzhvLHk89aoLeY-6r1m6CCWN727F2-VF1o)

When we added the file to our staging area, we essentially took a snapshot of the contents of test.txt. To take this snapshot, we create a blob object and generate a SHA-1 hash of the contents of the file, test.txt, for the key.   
  
Now, if you run “find .git/objects -type f” in the command line, it’ll come up with a file path that resembles a SHA-1 Hash — because that’s exactly what it is!

![](https://lh3.googleusercontent.com/cF5IViWlhmEIMB284_ka2oj8LzVrcTJ6K-ZUFrjO7OhKKVjFaXARsYOWgvtqpetQs6UpetYcPtumhXoz6q7PKybu6AaOBOj65TUukC_FkK_bjBrkZ_UCvsp8Jjgko0CHCkrj64A8EFr7hhdR8yQkvcE)

Note: Notice that Git split the hash into d6 & 70460… — Git names the subdirectory with the first 2 characters of the SHA-1, and the filename is the remaining 38 characters.

To check the value (or the content of the .git/object that we just found), we can use “git cat-file -p <HASH>”. The git cat-file command will display the contents of a git object, such as a blob in this example.

![](https://lh4.googleusercontent.com/PfVt5nHTvyvWzGNJdsgzKbpL0x7JZQJE2-p3P8BfvzH2BtrpFafoXeekI9fQV25lHi8qyif3Fmts-79UuDKvg3MpvNOMk97TTDLoTAPjBhHUmxcLysxCpM7rIMoRof0zoTsysDpKaz_hBFDDQaD0rmw)

In this example, we are operating at the lowest level of abstraction (tracking only files, not directories etc), so we are just taking the SHA-1 of files and creating blob objects. Can we now create a tree object?   
  
Of course! Recall that a commit object is merely a reference to a tree object. So, if we go ahead and “git commit” our staged changes, we’ll effectively not only create a tree object, but also a commit object!

![](https://lh6.googleusercontent.com/9O-nFHdoSvt0XcxHX1KlQy0eq5VIKenrjqk-rq7nSuT5qcxld5sfJQQgTgGeI5Nz1fFhmbM0mmjNyR-hZSUUFmIk5q8-7J5KJkyo0fe5KgC1rwPX_-z1AwIcP04Lq6Zp8LGLBbt5_aGCCk8UnkFFa40)

Here, we re-use the `git cat-file` command to explore the tree object that’s pointed to by the latest commit object on the main branch currently. Then, to explore the commit object that is pointing to the tree object, we can use `git rev-parse` to first find the hash of the latest commit on the main branch:

![](https://lh3.googleusercontent.com/TFpNZ5Uep8xO51WKXPQ9v5Veuz-yhUw1KXGqFoxW5yWKMlItcsmtFIKpp2uuIZ-qWtvNePpDLhOJRPvVlpG9a2iK9wbeNIjWNZ_uEVvFaVXBFC5uyB8J8F50-iReDKufVeRLw3ItFVdETcSmRCEEwzg)

Then we can use the `git cat-file` command again to display the contents of the commit object, using the SHA-1 object name obtained in the previous step:

![](https://lh5.googleusercontent.com/I1Vtyg1epDTq1UD8KfQpMt9DxleVQNgqv_WTt7Vjv0tMnQNH55_4BNOS8t_MxV8qvXuyG1-ZBmOkx5Ynj8UMOb2cDZEKn153doZYs6IehXtfRYArS-9BwTwBI6O82DntBzTHnhJbcPiL7ZkHwc330qo)

Finally, let’s list all of the git objects in our .git/objects folder by: 

As we expected, we see the 3 git objects we’ve explored using git plumbing commands: d6/704.. (blob), 80/865…(tree), 28/000… (commit). We can double-check these via the hashes we found in the previous steps. 

## **[Commits are snapshots, not diffs](https://github.blog/2020-12-17-commits-are-snapshots-not-diffs/)**

In conclusion, rather than a series of changes or diffs, commits in Git are snapshots of the repository at specific points in time as seen in the “snapshot” design of blob objects & its key-value store design. 

As we saw, this design choice allows Git to store the entire history of a repository efficiently, without having to track the changes (or diffs) made to each file individually. 

Having rebuilt (or built for the first time) our mental models of how git commits _actually_ work, we’ll be able to more effectively use this powerful tool in our daily work to supercharge development and make better decisions while building workflows for our teams!

## [Aviator](https://www.aviator.co/): Automate your cumbersome merge processes

[![](https://blog.aviator.co/wp-content/uploads/2022/08/blog-cta-1024x727.png)](https://aviator.co/)

Aviator automates tedious developer workflows by managing git Pull Requests (PRs) and continuous integration test (CI) runs to help your team avoid broken builds, streamline cumbersome merge processes, manage cross-PR dependencies, and handle flaky tests while maintaining their security compliance.

There are 4 key components to Aviator:

1. **MergeQueue**  – an automated queue that manages the merging workflow for your GitHub repository to help protect important branches from broken builds. The Aviator bot uses GitHub Labels to identify Pull Requests (PRs) that are ready to be merged, validates CI checks, processes semantic conflicts, and merges the PRs automatically.
2. **ChangeSets**  – workflows to synchronize validating and merging multiple PRs within the same repository or multiple repositories. Useful when your team often sees groups of related PRs that need to be merged together, or otherwise treated as a single broader unit of change.
3. **FlakyBot**  – a tool to automatically detect, take action on, and process results from flaky tests in your CI infrastructure.
4. **Stacked PRs CLI**  – a command line tool that helps developers manage cross-PR dependencies. This tool also automates syncing and merging of stacked PRs. Useful when your team wants to promote a culture of smaller, incremental PRs instead of large changes, or when your workflows involve keeping multiple, dependent PRs in sync.

[Try it for free.](http://aviator.co/)

The post [How do git commits actually work?](https://www.aviator.co/blog/how-do-git-commits-actually-work/) first appeared on [Aviator Blog](https://www.aviator.co/blog).

The post [How do git commits actually work?](https://www.aviator.co/blog/how-do-git-commits-actually-work/) appeared first on [Aviator Blog](https://www.aviator.co/blog).