Table of Contents
introduction
Most Git users are familiar with how each commit refers to one or more parents, except for the initial commit that has no parents.
As a result, commitment networks are created that we call branches. It also results in a structure known as a Directed Acyclic Graph or DAG, which is a graph structure that represents a series of nodes that relate to other nodes through directed edges, without circular structures or Cycles be possible
However, most Git users don’t realize that Git’s other core objects—trees and blobs—are interconnected and committed together using similar types of references.
In this article, we’ll discuss how Git commits connect to Git trees, and further how these trees connect to blobs that form a connected network of objects. This is what allows Git to do all the amazing tasks we developers use every day.
Git commits connect to root trees
As a casual Git user, you may not have known that every commit you make refers to a root tree. We have a whole article on trees if you need a refresher.
A root tree is just the top-level tree object that commit points to. Each commit points to one and only one root tree, indicating the collection of objects of which the commit is a “snapshot”. The root tree is created directly from the changes in the files in the staging area at the time of the commit.
Like all Git objects, each root tree is stored in the Git object database and identified by a unique SHA-1 hash value of its contents. This SHA-1 value is also known as Object ID. Each commit refers to its root tree by recording the object ID of the tree along with the contents of the commit.
A commit object has the format shown below, which includes the object ID of the root tree, along with the rest of the commit data:
commit <size-of-commit-data-in-bytes>' '
<root-tree-SHA1-hash>
<parent-1-commit-id>
<parent-2-commit-id>
...
<parent-N-commit-id>
author ID email date
committer ID email date
user comment
By storing the object ID of the root tree as part of the commit, Git can figure out what file content is associated with that commit, as we’ll see later.
Git root trees connect to blobs and subtrees
Since root trees in Git are just regular trees, they refer to the set of blobs and subtrees contained in a commit. Now would be a good time to brush up on stains, if you need one.
Here is the format of a tree as stored by Git:
tree <size-of-tree-in-bytes>