> For the complete documentation index, see [llms.txt](https://www.pranaypourkar.co.in/the-programmers-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pranaypourkar.co.in/the-programmers-guide/git/internals-of-git-tbd.md).

# Internals of Git

## Description

Git's internal workings is quite complex, involving a range of data structures and algorithms to manage the repository's history, branches, commits, and more. Here's a high-level overview of some key aspects of Git's internal workings.

## Object Model

The object model in Git is a core component that underpins how Git stores and manages data. In Git, everything is stored as an object, and these objects are content-addressable, meaning they are referenced by a hash of their content. The primary types of objects in Git are blobs, trees, commits, and tags. Here's a detailed look at each of these objects and how they work together.

### Types of Git Objects

#### **1. Blob (Binary Large Object)**

* **Purpose:** Represents the content of a file.
* **Content:** Stores the raw file data (i.e., the contents of a file).
* **Identification:** Identified by a SHA-1 hash of the file content.
* **Metadata:** Does not store any metadata such as the file name or permissions.

<figure><img src="https://3632859567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPd6ktrA5pPLsZJktj2fm%2Fuploads%2Fgit-blob-adfb15a632d1e1d51f0353c0798e9d671247866e%2Fimage.png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

Example of creating a blob:

```
echo "Hello, Git!" > hello.txt
git add hello.txt
# Creates a blob for hello.txt
```

#### **2. Tree**

* **Purpose:** Represents a directory and its contents.
* **Content:** Contains references (hashes) to blobs and other trees, along with the associated metadata (file names, types, and modes).
* **Identification:** Identified by a SHA-1 hash of its contents.

<figure><img src="https://3632859567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPd6ktrA5pPLsZJktj2fm%2Fuploads%2Fgit-blob-590fa04e222ba3c806d518f7689fdc37d9bede7e%2Fimage.png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

Example of creating a tree:

```
git write-tree
```

#### **3. Commit**

* **Purpose:** Represents a snapshot of the repository at a point in time.
* **Content:** Contains metadata (author, committer, message), a reference to a tree object (the state of the file system), and references to parent commits.
* **Identification:** Identified by a SHA-1 hash of its contents.

Example of creating a commit:

```
git commit -m "Initial commit"
```

#### **4. Tag**

* **Purpose:** Marks a specific commit as significant, often used to mark release points.
* **Content:** Contains metadata (tagger, message) and a reference to a commit object.
* **Identification:** Identified by a SHA-1 hash of its contents.

Example of creating an annotated tag:

```
git tag -a v1.0 -m "Version 1.0"
```

### How Objects are Stored

* **Content-Addressable Storage:**
  * Git uses SHA-1 hashes to uniquely identify each object. The SHA-1 hash is derived from the content of the object.
  * The content of each object is stored in the `.git/objects` directory in a subdirectory named after the first two characters of the hash, with the remaining characters as the filename.
* **Loose Objects and Packed Objects:**
  * **Loose Objects:** Individual files stored in the `.git/objects` directory. These are typically created during initial commits.
  * **Packed Objects:** Over time, loose objects are packed into packfiles to save space and improve performance. Packfiles are stored in the `.git/objects/pack` directory.

### Object Creation and Relationship

* **Blob Creation:**
  * When we add a file to Git (`git add`), Git creates a blob object containing the file's content.
* **Tree Creation:**
  * When you commit changes (`git commit`), Git creates a tree object representing the directory structure, containing references to blob objects for files and other tree objects for subdirectories.
* **Commit Creation:**
  * A commit object is created referencing the tree object and containing metadata about the commit (author, message, parent commits).
* **Tag Creation:**
  * A tag object is created to reference a specific commit, often annotated with additional information (tagger, message).

### Object Model Visualization

Here's a simple visualization of how these objects might be linked together.

<figure><img src="https://3632859567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPd6ktrA5pPLsZJktj2fm%2Fuploads%2Fgit-blob-69f5840935abaa85f1ae980c6dae70e1863944a8%2Fimage.png?alt=media" alt="" width="374"><figcaption></figcaption></figure>

### Inspecting Git Objects

```
-- List all objects
git rev-list --all --objects

-- View object type and size
git cat-file -t <object-hash>
git cat-file -s <object-hash>

-- View object content
git cat-file -p <object-hash>
```

Reference - <https://towardsdatascience.com/how-git-truly-works-cd9c375966f6>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://www.pranaypourkar.co.in/the-programmers-guide/git/internals-of-git-tbd.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
