Skip to main content

PyGit

Build your own version control system from scratch!

PyGit is a simplified version of Git, written entirely in Python. It teaches you how version control really works under the hood -- hashing, blobs, trees, commits, branches, and diffs -- by building each piece yourself.

What is version control?

Imagine you are writing a story. Every time you finish a chapter, you save a copy so you can go back to it later. Version control does the same thing for code -- it remembers every change you make, who made it, and why.

Quick start

from pygit.repository import Repository

# Create a new repository
repo = Repository.init("my-project")

# Create a file and stage it
with open("my-project/hello.txt", "w") as f:
    f.write("Hello, world!")

repo.add("hello.txt")

# Commit the change
commit_hash = repo.commit("Add greeting")
print(f"Created commit: {commit_hash}")

# Make another change
with open("my-project/hello.txt", "w") as f:
    f.write("Hello, everyone!")

repo.add("hello.txt")
repo.commit("Update greeting")

# See the history
for entry in repo.log():
    print(f"{entry.commit_hash[:8]} {entry.message}")

Features

  • Hashing -- Every piece of content gets a unique fingerprint using SHA-1, with Git's blob <size> label in front -- so file (blob) hashes match git hash-object exactly. (Trees and commits use a simpler JSON layout than real Git, so their hashes differ.)
  • Nested trees -- Every folder gets its own tree, just like real Git.
  • Object store -- Files are stored by their hash, so identical content is never duplicated.
  • Trees -- Snapshots of your directory structure, like a table of contents.
  • Commits -- Diary entries that record what changed, when, and why. Each commit points to its parent, forming a chain of history.
  • Branches -- Create parallel timelines to work on different ideas without affecting the main project.
  • Staging area -- Choose exactly which changes go into the next commit.
  • Diff -- Compare two versions of a file to see what was added or removed.
  • Status -- See what files are staged and ready to commit.

Installation

uv sync --all-extras

Running tests

uv run pytest

Project structure

src/pygit/
    __init__.py      -- Package marker
    hashing.py       -- SHA-1 fingerprinting
    objects.py       -- Content-addressed object store
    tree.py          -- Directory snapshots
    commit.py        -- History entries
    diff.py          -- Line-by-line comparison
    repository.py    -- Ties everything together
    errors.py        -- Custom exceptions

Releasing

Other projects in the series (PyStack) use a pinned release of this one, not whatever is on main. To publish a change for them:

  1. Merge the change to main.
  2. Tag it: git tag -a vX.Y.Z -m vX.Y.Z && git push origin vX.Y.Z. A change to the public API bumps the minor version (v0.1.0 → v0.2.0); anything else bumps the patch (v0.1.0 → v0.1.1).
  3. In each project that uses it, edit the tag in [tool.uv.sources], run uv lock --upgrade-package <name>, and open a pull request so CI checks the pair together.

This project is one of a series that builds real systems "from scratch in Python" to teach how they work. See the series home for the full story.

Project What it teaches Repository Docs
PyOS Operating systems py-os Docs
Pebble Compilers and programming languages pebble-lang Docs
PyDB Relational databases pydb Docs
PyKV Key-value stores pykv Docs
PyDocDB Document databases pydoc-db Docs
PyGraphDB Graph databases pygraphdb Docs
PyTSDB Time-series databases pytsdb Docs
PyVecDB Vector databases pyvecdb Docs
PyColDB Column-family databases pycoldb Docs
PyCrypt Cryptography pycrypt Docs
PyNet Computer networking pynet Docs
PyWeb HTTP web servers pyweb Docs
PyGit Version control pygit Docs
PySearch Search engines pysearch Docs
PyMQ Message queues pymq Docs
PyLLM Language models (the brain) pyllm Docs
PyScheduler Job scheduling pyscheduler Docs
PyStack Full-stack integration pystack Docs

Documentation

Full docs (with kid-friendly explanations of every idea): https://robcox2910.github.io/pygit/

License

MIT

Metadata

Release files for fromscratch-pygit 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fromscratch-pygit 0.3.0
File Size Uploaded Provenance
fromscratch_pygit-0.3.0.tar.gz 87.8 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for fromscratch-pygit 0.3.0
File Interpreter ABI Platform
fromscratch_pygit-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 105.0 kB

Release files / fromscratch_pygit-0.3.0.tar.gz

Download URL fromscratch_pygit-0.3.0.tar.gz
Size 87.8 kB
Tags Source
SHA-256 checksum
How to use checksums
be9542d6f063220a82493b01b90df67744925353cdf28365c894856245357555
BLAKE2b-256 checksum
How to use checksums
507abea6c05bbdc4b32ad9859e056be6da4bba20cd2ce2333b1cd7d59dffdf61
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.

Transparency log

Release files / fromscratch_pygit-0.3.0-py3-none-any.whl

Download URL fromscratch_pygit-0.3.0-py3-none-any.whl
Size 17.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6b3d2593b95b414de57f788c17bfe8894c8c9cff88f9490ef1ef879b5c57eabd
BLAKE2b-256 checksum
How to use checksums
bae0f40f4b22bb6d61dd30c74c943a754b4c7de3b6668256bab00810588d141c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.

Transparency log

Security for fromscratch-pygit 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for fromscratch_pygit-0.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page