DEV Community

Cover image for Mastering Git Internals: Blobs, Trees, Commits, and Resolving Ugly Merge Conflicts
DEVANSHU PATIL
DEVANSHU PATIL

Posted on AI-assisted

Mastering Git Internals: Blobs, Trees, Commits, and Resolving Ugly Merge Conflicts

Mastering Git Internals: Blobs, Trees, Commits, and Resolving Ugly Merge Conflicts

Almost every software engineer uses Git every single day:

git add .
git commit -m "fix bug"
git push origin main
Enter fullscreen mode Exit fullscreen mode

Yet to many developers, Git feels like a mysterious black box. When an ugly merge conflict or detached HEAD state occurs, panic sets in, and people resort to deleting the folder and re-cloning the repository.

Git is not an opaque mystery. At its core, Git is a remarkably simple, elegant content-addressable key-value object database paired with a Directed Acyclic Graph (DAG).

In this deep dive, we explore what actually lives inside the .git directory and master how Git resolves conflicts.

The 4 Core Object Types in .git/objects

Every file, folder, and commit in Git is stored as an immutable object compressed with zlib. The filename is the SHA-1 (or SHA-256) hash of its content.

                     +-----------------------------+
                     | Commit Object               |
                     |  - Author / Timestamp       |
                     |  - Parent: a1b2c3d          |
                     |  - Tree: 7f8e9a0 (Root Dir) |
                     +-----------------------------+
                                    |
                                    v
                     +-----------------------------+
                     | Tree Object (Directory)     |
                     |  - 100644 blob e69de29 main.py
                     |  - 040000 tree 4b825dc src/ |
                     +-----------------------------+
                                    |
                                    v
                     +-----------------------------+
                     | Blob Object (File Content)  |
                     |  "print('Hello, Git!')"     |
                     +-----------------------------+
Enter fullscreen mode Exit fullscreen mode
  1. Blob: Stores raw file content. (No filename or permissions).
  2. Tree: Represents a directory with filenames and mode permissions.
  3. Commit: Contains commit message, author info, timestamp, pointer to root Tree, and Parent Commits.

Resolving Ugly Merge Conflicts with git rerere

One of Git's best-kept secrets is rerere (Reuse Recorded Resolution).

If you rebase long-lived feature branches, you often have to resolve the exact same merge conflict multiple times.

Enable rerere globally:

git config --global rerere.enabled true
Enter fullscreen mode Exit fullscreen mode

How it Works:

  1. When a merge conflict occurs, Git records a snapshot of the conflict.
  2. When you manually resolve it and commit, Git records the resolution.
  3. In future rebases or merges, whenever the exact same conflict occurs anywhere in the repository, Git automatically resolves it for you!

Top comments (0)