Project 13: Git from Scratch

A Git implementation that handles the core commands: init, add, commit, log, branch, checkout, merge—using the same object format as real Git, so you can interoperate with the real git CLI.

Quick Reference

Attribute Value
Primary Language Go
Alternative Languages Rust, C, Python
Difficulty Level 4: Expert
Time Estimate 1 month
Knowledge Area Version Control, Content-Addressable Storage, Graphs
Tooling None (from scratch)
Prerequisites Completed Projects 1-6. Strong understanding of file systems. Familiarity with how Git works conceptually.

What You Will Build

A Git implementation that handles the core commands: init, add, commit, log, branch, checkout, merge—using the same object format as real Git, so you can interoperate with the real git CLI.

Why It Matters

This project builds core skills that appear repeatedly in real-world systems and tooling.

Core Challenges

  • Object storage (blobs, trees, commits) → maps to hashing, file I/O, compression
  • Index (staging area) → maps to binary format parsing, file locking
  • Branch management → maps to refs, symbolic links, DAG traversal
  • Merge algorithms → maps to three-way merge, conflict detection

Key Concepts

  • Git internals: “Pro Git” Ch. 10 - Scott Chacon (free online)
  • SHA-1 hashing: crypto/sha1 package
  • Zlib compression: compress/zlib package
  • DAG traversal: Algorithm fundamentals

Real-World Outcome

$ ./minigit init
Initialized empty Git repository in .git/

$ ./minigit status
On branch master
No commits yet
nothing to commit

$ echo "Hello, World!" > hello.txt
$ ./minigit add hello.txt
$ ./minigit status
On branch master
No commits yet
Changes to be committed:
  new file: hello.txt

$ ./minigit commit -m "Initial commit"
[master (root-commit) a3f4b21] Initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 hello.txt

# Real git can read our repository!
$ git log --oneline
a3f4b21 Initial commit

$ git cat-file -p a3f4b21
tree 8ab686ea...
author You <you@example.com> 1704891234 -0500
committer You <you@example.com> 1704891234 -0500

Initial commit

$ ./minigit branch feature
$ ./minigit checkout feature
Switched to branch 'feature'

$ echo "New feature" >> hello.txt
$ ./minigit add hello.txt
$ ./minigit commit -m "Add feature"
[feature 7c8d9e0] Add feature

$ ./minigit checkout master
$ ./minigit merge feature
Merge made by recursive strategy.

Implementation Guide

  1. Reproduce the simplest happy-path scenario.
  2. Build the smallest working version of the core feature.
  3. Add input validation and error handling.
  4. Add instrumentation/logging to confirm behavior.
  5. Refactor into clean modules with tests.

Milestones

  • Milestone 1: Minimal working program that runs end-to-end.
  • Milestone 2: Correct outputs for typical inputs.
  • Milestone 3: Robust handling of edge cases.
  • Milestone 4: Clean structure and documented usage.

Validation Checklist

  • Output matches the real-world outcome example
  • Handles invalid inputs safely
  • Provides clear errors and exit codes
  • Repeatable results across runs

References

  • Main guide: LEARN_GO_DEEP_DIVE.md
  • “Pro Git” by Scott Chacon (free online)