Sub topic 1
Where Git keeps your data: the object database
Prerequisites
This chapter assumes you can run git init, git add, git commit, and git log. It also assumes you are comfortable in a Unix-style shell and know what a cryptographic hash function does: it turns any input into a fixed-length fingerprint, and the same input always gives the same fingerprint.
The working model
Git is a content-addressable store with a version control interface on top. Everything Git remembers about your project lives in one directory, .git, at the root of the repository. The part that holds your data is the object database, stored under .git/objects.
The object database holds four kinds of objects:
- A blob holds the contents of a file, without its name.
- A tree holds a directory listing: names, modes, and pointers to blobs and other trees.
- A commit records a snapshot (a pointer to a tree), its parent commits, and metadata.
- An annotated tag is a labeled pointer to another object, with its own message.
Later chapters cover each type. Here you only need to see where they live and how they are named.
A fresh repository
Create an empty repository and look at what is inside .git.
git init demo && cd demo
ls .git
find .git/objectsYou will see entries such as HEAD, config, objects, and refs. The objects directory contains only two empty subdirectories, info and pack. No objects exist yet, because nothing has been stored.
Now store one piece of content.
echo 'hello' | git hash-object -w --stdin
find .git/objects -type fThe first command prints a 40-character hexadecimal ID. The second shows one new file. The path of that file is built from the ID: the first two characters become a subdirectory name, and the remaining 38 become the file name. For example, an ID starting with ce013625 is stored at .git/objects/ce/013625....
Splitting on the first two characters keeps any single directory from holding too many files. There are at most 256 such subdirectories.
How the database is organized
Three parts matter for this tutorial:
| Location | What it holds |
|---|---|
| `.git/objects/xx/yyyy...` | One file per loose object, named by its ID |
| `.git/objects/pack/` | Packfiles, which store many objects compactly |
| `.git/refs/` and `.git/HEAD` | Names that point to commits, such as branches |
Objects and references are separate. Objects are found by their ID and never change. References are small, movable names that point at objects. Without a reference, you would have to remember raw IDs.
Why the name comes from the content
The ID of an object is computed from its content, not chosen by Git or by you. This has direct consequences:
- Two files with identical content produce the same ID, so Git stores them once.
- If one byte changes, the ID changes, so Git stores a new object and leaves the old one alone.
- The ID works as an integrity check. Git can recompute the hash and detect corruption.
The next chapter explains how the ID is computed.
Common errors
- Running
git hash-objectwithout-wand expecting a file in.git/objects. Without-w, Git only prints the ID and writes nothing. - Looking for file names inside
.git/objects. Names are not stored in blobs. They live in trees. - Running the commands outside a repository.
git hash-object -wneeds a repository to write into, and fails with a "not a git repository" message otherwise.
Exercise
You run echo 'hello' | git hash-object -w --stdin and get an ID beginning with ce013625. What is the path of the new file inside the repository, and what happens if you run the same command a second time?
Show answer
The file is .git/objects/ce/013625..., where the first two characters of the ID name the subdirectory and the remaining 38 characters name the file. Running the command again prints the same ID and stores nothing new, because the ID depends only on the content and the object already exists.
Exercise
Why does git hash-object --stdin without -w leave .git/objects unchanged?
Show answer
Without -w, the command only computes and prints the object ID. The -w flag tells Git to write the object into the database.