Git Internals
Under the friendly commands, Git is a simple content-addressable database: a key-value store where the key of each object is the SHA-1 hash of its content. Understanding the four object types demystifies almost everything else (branches, merges, rebases).
| Object | Stores | Points to |
|---|---|---|
| blob | the content of one file (no name!) | nothing |
| tree | a directory listing: names, modes and hashes | blobs and other trees |
| commit | a snapshot: one tree + parents + author + message | a tree and parent commit(s) |
| tag (annotated) | a name, tagger and message | usually a commit |
Branches, HEAD and remote-tracking branches are not objects. They are small files (refs) containing a hash.
Blobs: content and its hash
git hash-object computes the ID Git would give a file's content (--stdin reads the content from a pipe instead); -w also writes it into the database. The same content always gets the same ID, on every computer:
mkdir demo && cd demo && git init -q
echo "hello git" > a.txt
cp a.txt b.txt
git hash-object a.txt b.txt
echo "--- the hash is SHA-1 of a header + the content:"
printf 'blob 10\0hello git\n' | sha1sum8d0e41234f24b6da002d962a26c2495ea16a425f
8d0e41234f24b6da002d962a26c2495ea16a425f
--- the hash is SHA-1 of a header + the content:
8d0e41234f24b6da002d962a26c2495ea16a425f -
Two files with identical content are stored once. The ID is the SHA-1 of blob <size>\0<content>, which you can reproduce with sha1sum.
Looking inside a commit
git cat-file -t prints an object's type and -p pretty-prints it. Follow the chain from a commit to its tree to a blob:
mkdir demo && cd demo && git init -q
mkdir src
echo "print('hi')" > src/main.py
echo "# Demo" > README.md
git add . && git commit -q -m "Initial commit"
echo "== commit:"; git cat-file -p HEAD
echo "== root tree:"; git cat-file -p HEAD^{tree}
echo "== src tree:"; git cat-file -p HEAD:src
echo "== blob:"; git cat-file -p HEAD:src/main.py
echo "== types:"; git cat-file -t HEAD; git cat-file -t HEAD:src; git cat-file -t HEAD:README.md== commit:
tree 8305d39541e96e94171de71ccbe75f384516dc3b
author Ada Lovelace <ada@example.com> 1768471200 +0000
committer Ada Lovelace <ada@example.com> 1768471200 +0000
Initial commit
== root tree:
100644 blob 0805455a24b6c68fbc38d0fa5d121f735984285d README.md
040000 tree 6709938ce5ee5edd4b966582901952f48a83b7d1 src
== src tree:
100644 blob 9f1b437537a2acdadafd3174f6f0af9c1a04f5e4 main.py
== blob:
print('hi')
== types:
commit
tree
blob
The commit holds a tree hash, the author/committer lines (the number is a Unix timestamp) and the message. Trees list entries with mode 100644 (file), 100755 (executable), 040000 (folder) or 160000 (submodule).
Where objects live
Objects are stored compressed in .git/objects/, in a folder named after the first two hex digits of the hash. Refs are plain text files:
mkdir demo && cd demo && git init -q
echo "hello git" > a.txt && git add . && git commit -q -m "First"
find .git/objects -type f | sort
echo "--- refs:"
cat .git/HEAD
cat .git/refs/heads/main
git count-objects -v | grep "^count".git/objects/8d/0e41234f24b6da002d962a26c2495ea16a425f
.git/objects/9d/e83f16afb4f118c5e5892bf15157fdbb09432e
.git/objects/c5/723c0d95315a8739ca137d83ed1ec4ebbdd1de
--- refs:
ref: refs/heads/main
9de83f16afb4f118c5e5892bf15157fdbb09432e
count: 3
Three objects: the blob, the tree and the commit. HEAD contains ref: refs/heads/main (a symbolic ref), and the branch file contains the commit hash. Later, git gc packs loose objects into compressed .pack files.
Building a commit by hand
With plumbing commands you can do everything git add and git commit do, step by step. This is exactly what they do internally:
mkdir demo && cd demo && git init -q
echo "made by hand" > ~/draft.txt # a file outside the repo
blob=$(git hash-object -w ~/draft.txt) # store it as a blob
git update-index --add --cacheinfo 100644 "$blob" note.txt # stage it
tree=$(git write-tree) # index -> tree
commit=$(echo "Handmade commit" | git commit-tree "$tree") # tree -> commit
git update-ref refs/heads/main "$commit" # move the branch
echo "blob $blob"
echo "tree $tree"
git log --oneline
git show --stat --format=%s HEADblob a6adc7fae93201dec933881caf4c4370d50553ab
tree 9a9214250d2604fa99c67df17c399b1adff35e68
36c9693 Handmade commit
Handmade commit
note.txt | 1 +
1 file changed, 1 insertion(+)
git status would now show note.txt as deleted from the working folder, because the file exists only in the database: we never wrote it to disk.
Why this matters
- Integrity: changing any byte of history changes the hashes of everything above it, so tampering and corruption are detectable (
git fsck). - Cheap branches: a branch is a 41-byte file, so creating one is instant.
- Rebase/amend/cherry-pick make new commits: commits are immutable, because their ID is their content.
- Deduplication: identical files across commits and branches are stored once.
Note: New repositories can use SHA-256 instead of SHA-1 (
git init --object-format=sha256). The concepts are identical; hashes are 64 hex digits instead of 40.
Test yourself
Which object type stores a file's name?
- blob
- tree
- commit
- ref
Two different files in a repository have exactly the same content. How many blobs does Git store?
- Two
- One
- None
- One per commit
What is a branch, physically?
- A copy of all files
- A small file under .git/refs/heads containing a commit hash
- A tree object
- A compressed pack file