# Inside \.git

Git's object model with real objects: blobs, trees, commits, refs, HEAD, a merge, a rebase and the reflog, each card printing its exact git cat\-file bytes

By Numa

Topic: Computing, AI &amp; Electronics

[Open the interactive Jig](https://ask.jig.com/j/cYs8NodqJ8As)

## Transcript

### 1. Git is a content\-addressed object store

Every Git repository is a key\-value store of immutable objects\. The key is the SHA\-1 of the object's bytes, so the content decides the name\. This repository holds 23 objects: 6 commits, 10 trees and 7 blobs\. Click any object to follow the hashes it contains\.

The fourth object type, the annotated tag, does not appear in this history\.

### 2. A blob is a header plus the file's bytes

git add hashes the header "blob 7" and a NUL byte, followed by the file's 7 bytes\. The SHA\-1 of those 14 bytes names the object\. Its first two hex digits pick the directory in \.git/objects, where the bytes are stored zlib\-compressed\. The blob holds no file name and no mode\.

Two files with identical bytes, anywhere in history, are one blob\.

### 3. Trees store names, modes and hashes

A tree lists one entry per name: mode, type, hash and name, sorted by name\. A subdirectory is an entry with mode 040000 that points at another tree\. The hash in each entry is the pointer, and it is all Git needs to find the object\.

Renaming a file writes new trees; its blob is untouched\.

### 4. A commit points at one root tree

A commit records its root tree, its parent commits, the author and committer with a Unix timestamp and time zone, and the message\. That root tree is the whole project at that moment\. The first commit has no parent line\.

The hash covers every byte of the commit, so a different parent, time or message gives a different hash\.

### 5. One edit, four new objects

Changing src/main\.c writes a new blob, a new src tree, a new root tree and a commit whose parent is A\. README\.md and src/util\.c are referenced by their existing hashes, not copied\. Every commit is a full snapshot; diffs are computed when you ask for them\.

git gc packs objects and may store some as deltas\. That is compression only: hashes and cat\-file output stay the same\.

### 6. A branch is a 41\-byte file

refs/heads/main holds one commit hash and a newline\. HEAD names the current branch, as in ref: refs/heads/feature\. A commit writes its objects, then moves only the branch HEAD names: feature advances to C and D while main stays on B, then main advances to E\.

In detached HEAD, \.git/HEAD holds a raw commit hash instead of a ref\.

### 7. A merge is a commit with two parents

git merge feature writes a commit whose first parent is main's tip, E, and whose second parent is feature's tip, D\. Its tree combines E's README\.md blob with D's src tree, so the merge adds only one root tree and the commit itself\.

The repository now holds 23 objects: 6 commits, 10 trees and 7 blobs\.

### 8. Every commit names a complete snapshot

Choose a commit to light its snapshot: its root tree and every tree and blob below it\. Each snapshot is complete, yet unchanged content is shared: src/util\.c is the single blob 39a290b in all six commits\.

### 9. Rebase writes new commits

Instead of merging, git rebase main replays C and D onto E\. A new parent changes a commit's bytes, so the copies C' and D' get new hashes, and C and D stay exactly as they were\. D' and the merge M share one tree, 10cfcc3: same content, same hash\.

Rebase keeps each author line and writes a new committer timestamp\.

### 10. The reflog keeps the old tips

After the rebase, no branch reaches C or D\. \.git/logs/HEAD and ORIG\_HEAD still record d8fa3ac, so git reset \-\-hard ORIG\_HEAD restores the old tip\. By default, git gc expires reflog entries for unreachable commits after 30 days; only then can it prune those objects\.

The reflog is local to your clone and is never pushed\.
