A small Git implementation in C++20, written to understand what actually happens inside
git init, git add, git commit, git log, and friends.
The object format is byte-for-byte compatible with real Git. A blob written by MyGit has
the same SHA-1 as git hash-object, the tree written by mygit commit matches git write-tree,
and git cat-file can read MyGit's commits and trees. The test suite checks this on every run.
When I was watching a YouTube video about job interviews, one candidate's CV mentioned that they had implemented Git from scratch. I thought it was very cool, so I decided to give it a try.
MyGit is a learning project, not a production replacement for Git. Every shortcut is documented, and the goal is to make the core ideas — content-addressed storage, trees, commits, refs, and the staging area — visible in a small, readable codebase.
| Command | What it does |
|---|---|
mygit init |
Create .mygit/ (objects, refs, HEAD, config) |
mygit add <path>... |
Hash files into blobs and update the index (directories are recursive) |
mygit commit -m <message> |
Write a tree and a commit, then advance refs/heads/main |
mygit status |
Show staged, unstaged, and untracked files |
mygit log |
Walk the parent chain and print history |
mygit diff [--cached] |
Line diff of unstaged changes (staged changes with --cached) |
mygit cat-file -p/-t/-s <hash> |
Inspect an object; accepts HEAD, branch names, and short hashes |
mygit hash-object [-w] <file> |
Compute (and optionally store) a blob hash |
mygit config user.name/user.email |
Read and write repository config |
Git is a content-addressed database. Everything is one of three object types:
flowchart LR
W[Working tree] -- "mygit add" --> I[Index / staging area]
I -- "mygit commit" --> T[Tree]
T --> C[Commit]
C --> R[refs/heads/main]
- Blob — the contents of one file. The filename is not stored in the blob.
- Tree — one directory: a sorted list of
(mode, name, hash)entries. An entry points to either a blob or another tree, which is how directories nest. - Commit — a snapshot: one tree hash, zero or more parent commits, and author/committer metadata.
Every object is serialized as
<type> <size>\0<content>
then SHA-1 hashed and zlib-compressed. The hash determines the path:
.mygit/objects/<first 2 hex chars>/<remaining 38 hex chars>
For example, a file containing hello\n becomes the blob
blob 6\0hello\n
which hashes to ce013625030ba8dba906f756967f9e9ca394464a. Real Git agrees:
$ printf 'hello\n' | git hash-object --stdin
ce013625030ba8dba906f756967f9e9ca394464a
$ mygit hash-object hello.txt
ce013625030ba8dba906f756967f9e9ca394464aTrees use Git's binary encoding: <mode> <name>\0 followed by 20 raw bytes of SHA-1. Entries are
sorted by name, with directories compared as if their name ended in /. Commits are plain text:
tree <tree-hash>
parent <parent-hash>
author Andy <andy@example.com> 1796031224 +0800
committer Andy <andy@example.com> 1796031224 +0800
Initial commit
.mygit/indexis the staging area. Real Git uses a binary index; MyGit uses a simple text file with<hash> <path>on each line..mygit/refs/heads/maincontains the hash of the latest commit onmain..mygit/HEADcontainsref: refs/heads/main. A branch is just a movable pointer to a commit.
Requires CMake 3.16+, a C++20 compiler, OpenSSL, and zlib.
cmake -S . -B build
cmake --build build --parallel
./build/mygit init
./build/mygit config user.name "Your Name"
./build/mygit config user.email "you@example.com"
printf '# demo\n' > README.md
mkdir src && printf 'int main() { return 0; }\n' > src/main.cpp
./build/mygit add .
./build/mygit status
./build/mygit commit -m "Initial commit"
./build/mygit logA short session:
$ mygit add .
staged README.md (blob c804358)
staged src/main.cpp (blob 76e8197)
$ mygit status
On branch main
Changes to be committed:
new file: README.md
new file: src/main.cpp
$ mygit commit -m "Initial commit"
[main a6d591f] Initial commit
$ mygit log
commit a6d591ff2738156b2c62fd87437020d0e8495ffd
Author: Andy <andy@example.com>
Date: Mon Nov 30 17:33:44 2026 +0800
Initial commit
Inspecting the objects is the most useful part:
$ mygit cat-file -p HEAD
tree fe62d86d5bad3aa23e0130c60f5c100556837db8
author Andy <andy@example.com> 1796031224 +0800
committer Andy <andy@example.com> 1796031224 +0800
Initial commit
$ mygit cat-file -p fe62d86
100644 blob c804358a305c9eba235f443437be0edc7135c9a2 README.md
040000 tree ea6b23b5c4af9fd195cbad207fb36e119743e8a9 src
The tests cross-check MyGit against Git on every run:
mygit hash-object <file>equalsgit hash-object <file>- the tree produced by
mygit commitequalsgit write-treefor the same files git cat-filecan read MyGit objects after.mygit/objects/is copied into a real repository
That last point is the fun one: these are not "Git-like" objects, they are valid Git objects.
Note that MyGit itself works on .mygit/, not .git/; only the object store is interchangeable.
include/mygit/
util.hpp SHA-1, zlib, and file helpers
object_store.hpp loose objects and tree (de)serialization
repository.hpp repository operations
src/
util.cpp
object_store.cpp
repository.cpp
main.cpp command-line parsing
tests/
test_mygit.sh end-to-end tests, including Git cross-checks
This is deliberately a small implementation. It leans on C++20 standard-library
facilities where they make the intent clearer — std::filesystem, std::span,
std::ranges::sort, std::mismatch, and std::from_chars — instead of
reimplementing algorithms by hand. Known differences from Git:
- The index is a text file rather than Git's binary index, so there is no stat cache or file-mode tracking.
- Every file is stored as mode
100644; the executable bit is not preserved. - No
.gitignoresupport yet, somygit add .stages everything, includingbuild/. - No branches, checkout, reset, merge, tags, packfiles, or remotes.
mainis the only branch. difftrims the common prefix and suffix and prints one hunk; it is not a minimal diff and does not group distant changes like Git.- Paths are handled as UTF-8 strings; unusual platform path encodings are not covered.
Each of these keeps the core object model small and visible.
cmake -S . -B build
cmake --build build --parallel
cd build && ctest --output-on-failureCI runs the same suite on Ubuntu.
-
init,add,commit,log,status,diff,cat-file,hash-object - Git-compatible loose objects and trees
- Nested directories
- Branches and
checkout -
restore --stagedandreset - Three-way merge
-
.gitignoresupport - Packfiles
- Executable bit and file modes