A Git-compatible version control system written from scratch in C++20.
The project is inspired by the Git resources collected in Codecrafters' Build Your Own X repository, but the implementation is original C++ and focuses on Git's real storage and transport formats: content-addressed objects, zlib compression, the v2 index, trees, commits, refs, packfiles, delta objects, pkt-line framing, and Git smart HTTP.
init— create a Git-compatible repository layouthash-object [-w]— compute and optionally store Git blob objectscat-file -p|-t|-s— inspect objectsadd— stage files into a Git index v2 filewrite-tree/ls-tree— serialize and inspect treescommit-tree/commit -m— create commitslog— traverse commit historybranch— list and create branchescheckout— switch branches and materialize treesstatus— detect tracked working-tree modificationsdiff— basic line-oriented working-tree diffunpack-pack— parse a Git PACK stream into loose objectsfetch <url> [ref]— discover a remote over smart HTTP, request a pack, resolve deltas, and update a refclone <url> [directory]— perform a read-only smart-HTTP clone and check out the remote HEAD
Git object IDs are computed exactly as Git does:
SHA1("<type> <size>\0" + payload)
Loose objects are zlib-compressed into .git/objects/aa/....
cppgit parses PACK v2/v3 streams, validates the trailing SHA-1 checksum, inflates packed objects, and resolves both Git delta formats:
OFS_DELTA— base object addressed by backward pack offsetREF_DELTA— base object addressed by SHA-1
Resolved objects are materialized into the normal loose-object database, so the rest of cppgit does not need a second read path.
Remote discovery uses:
GET <remote>/info/refs?service=git-upload-pack
cppgit parses pkt-lines and advertised refs/capabilities, including symref=HEAD:..., then requests the desired commit from git-upload-pack using side-band transport and extracts the returned PACK stream.
The network layer intentionally targets public HTTPS smart-HTTP repositories. Authentication, protocol v2, SSH transport, shallow clones, and negotiation against existing local history are not implemented yet.
┌──────────────────┐
Remote Git server ── HTTP ──► │ pkt-line parser │
└────────┬─────────┘
│
▼
┌──────────────────┐
│ PACK + deltas │
└────────┬─────────┘
│ resolved objects
▼
Working tree .git/objects (loose object DB)
│ │
├── Index (DIRC v2) ───────────┤
│ ▼
│ Tree objects
│ │
▼ ▼
Blob objects Commits
│
▼
refs/heads/* + HEAD
Requirements:
- C++20 compiler
- CMake 3.20+
- OpenSSL
- zlib
- libcurl
- pkg-config on Unix-like systems
Ubuntu/Debian:
sudo apt install build-essential cmake pkg-config libssl-dev zlib1g-dev libcurl4-openssl-dev
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build
ctest --test-dir build --output-on-failureWindows with Visual Studio + vcpkg:
vcpkg install openssl:x64-windows zlib:x64-windows curl:x64-windows pkgconf:x64-windows
cmake -S . -B build `
-DCMAKE_TOOLCHAIN_FILE=$env:VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake
cmake --build build --config Release
ctest --test-dir build -C Release --output-on-failureCreate local history:
mkdir demo && cd demo
/path/to/cppgit init
echo "hello" > hello.txt
/path/to/cppgit add hello.txt
/path/to/cppgit commit -m "initial commit"
/path/to/cppgit logClone a public smart-HTTP Git remote:
cppgit clone https://github.com/octocat/Hello-World.git
cd Hello-World
cppgit logFetch a branch into an existing repository:
cppgit fetch https://github.com/octocat/Hello-World.git masterThe project deliberately emits real Git formats. Native Git can inspect repositories and objects produced by cppgit:
git cat-file -p HEAD
git ls-tree HEAD
git statusFor packfile compatibility, the integration test creates history with native Git, asks Git to generate a deltified PACK stream, unpacks it through cppgit, and verifies every resulting object against native Git:
./tests/pack_interop.sh ./build/cppgitThe test corpus currently exercises non-delta objects plus delta chains of multiple levels.
This is an educational implementation, not a production Git replacement. Important omissions include:
- push / receive-pack
- SSH and
git://transports - HTTP authentication / credential helpers
- Git protocol v2
- shallow and partial clones
- efficient fetch negotiation against existing history
.idxpack-index reading and direct packed-object lookup- merge and merge-base
- rename detection
- submodules
- reflogs
- advanced index extensions
- full config parsing
- production-grade checkout conflict handling
- Git's full Myers/patience diff behavior
The project idea comes from the Git section of codecrafters-io/build-your-own-x, which links guides such as Write yourself a Git!, ugit, Gitlet, and other bottom-up Git implementations. Those resources are learning references; this codebase implements the concepts independently in C++20.