rgoussu@goussu: ~/library/system-administration/exercises
~/library/system-administration/exercises cat write-yourself-a-git.md

Write Yourself a Git

# Reimplement Git's core from the object store up — blobs, trees, commits, and refs — until the content-addressed model is muscle memory.

Exercisesaved 2026-08-08source #git#internals#version-control#exercise#systems-programming

Goal

Build a small Git-compatible tool that reads and writes the real .git object store. Implementing hashing, object serialization, trees, commits, and refs by hand makes the data model concrete: you see that a commit is a snapshot, a branch is a 41-byte pointer, and nothing is magic — every porcelain command is manipulation of objects and refs.

Subject: full brief & instructions

Practices

  • Git internals — the object types, refs, the index, and the reflog that this exercise reconstructs from scratch.
  • Cryptography for engineers — content-addressing is SHA-1/SHA-256 hashing used for identity and tamper-evidence, not secrecy.

Milestones

  1. The object store. Implement hash-object and cat-file: read a file, prepend the blob <size>\0 header, zlib-compress, SHA it, and store under .git/objects/ab/cdef…. Round-trip a blob you wrote against real git cat-file.
  2. Reading trees & commits. Parse tree objects (mode, name, hash entries) and commit objects (tree, parents, author, message); implement ls-tree and log walking parent pointers.
  3. Writing trees & commits. Implement write-tree from a directory and commit-tree to snapshot it with a parent and message — build a commit real Git can then checkout.
  4. Refs & HEAD. Read and write refs under .git/refs/, resolve HEAD (symbolic and detached), and implement checkout of a commit into the working tree.
  5. Interop proof. Create commits with your tool, then inspect and continue history with stock git — and vice versa — confirming byte-compatible objects.

Stretch goals

  • Implement the index and a real add/status (worktree↔index↔HEAD diffs).
  • Read packfiles, or implement a basic three-way merge.

Related