Skip to content

BuildWithAbid/codecrafters-git-python

Repository files navigation

progress-banner

Build Your Own Git in Python — a working Git client from scratch

A minimal but real Git implementation in pure Python. It can init a repository, hash and inflate objects, build trees from a working directory, create commits, and clone a real repository over the smart HTTP protocol — pack files, deltas, and all. No libgit2, no git binary, no third-party Git libraries.

Built in under 48 hours with Claude Code while reaching CodeCrafters Python leaderboard rank #13.

If you searched for "how does git work internally", "git objects in Python", "git packfile format", or "implement git from scratch" — this repo is a complete, readable reference.


What works

$ mkdir demo && cd demo
$ /path/to/your_program.sh init
Initialized git directory

$ /path/to/your_program.sh hash-object -w README.md
a1b2c3...

$ /path/to/your_program.sh ls-tree --name-only <tree-sha>
README.md
src

$ /path/to/your_program.sh clone https://github.com/codecrafters-io/git-sample-1 cloned

Supported commands

  • init — creates .git/ with objects/, refs/, and HEAD
  • cat-file -p <sha> — zlib-inflate and print an object
  • hash-object [-w] <file> — compute SHA-1 and optionally write the blob
  • ls-tree [--name-only] <tree> — parse and list tree entries
  • write-tree — recursively hash a working directory into a tree object
  • commit-tree <tree> -p <parent> -m <msg> — build a commit object
  • clone <url> <dir> — real smart-HTTP clone:
    • GET /info/refs?service=git-upload-pack + pkt-line parsing
    • POST /git-upload-pack with want/done negotiation
    • pack file parsing (PACK header, entry types 1–7)
    • delta resolution (ref-delta + ofs-delta, with varint offsets and delta-instruction decoding)
    • tree checkout to the working directory

How it's built

  • Zero dependencies beyond the standard library (zlib, hashlib, struct, urllib)
  • One file (app/main.py, ~380 lines) — each Git concept maps to a function you can read top-down

Run it

./your_program.sh <command> [args...]

Requires uv and Python 3.14.

⚠️ your_program.sh operates on the .git in your current directory. Run it inside a scratch folder (e.g. /tmp/testing) so you don't touch the real repo.

alias mygit=/path/to/your/repo/your_program.sh
mkdir -p /tmp/testing && cd /tmp/testing
mygit init

Why this repo is worth reading

Git's storage model (content-addressed blobs → trees → commits) becomes obvious once you've written it in ~400 lines. The clone path in particular — pkt-lines, packfile format, delta reconstruction — is the part most tutorials skip, and it's all here.


Credits

Part of the CodeCrafters "Build Your Own Git" challenge.

About

A Git client in pure Python — objects, trees, commits, and a real smart-HTTP clone with packfile & delta decoding. Built in under 48 hours with Claude Code (CodeCrafters Python rank #13).

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors