Git implemented from the ground in 500 lines of code (or less)...
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

KBGit - Git implemented from scratch in 500 lines of code (or less ...)

Project statistics: Stats Stats

Recently I dug into their implementation details of Git. The simplicity of the implementation was completely mind-boggling. What a gem I found! Such a simple model for something conceptually complex as a source code versioning system. I almost couldn't believe it!

I wanted to share the beauty of the details of Git's inner workings. So how to best do this? I found inspiration from Terry A. Davis' work on TempleOS

Everybody is obsessed, Jedi mind-tricked, by the notion that when you scale-up, it doesn't get bad, it gets worse. They automatically think things are going to get bigger. Guess what happens when you scale down? It doesn't get good, it gets better! -- Terry A. Davis (

So why would you want to grok this code? The reward reaped is that you'll find many of the operations of Git much more natural. They will suddenly make sense. In order to have a fighting chance of conveying anything to any reader (and as Terry says). Less is more. I challenged myself to keep the implementation to a maximum of 500 lines of code.. for a "complete re-implementation of git"!

I want a fair game, though. And 500 lines of code is not a lot. To prevent myself from spiraling into an code-obfuscation contest in order to save a few lines of code, I'm counting lines using a simple line counting library which count only semantic lines (i.e. exclude empty lines, lines only containing {, },...).

KBGit..what?? My initials are K.B.G. - hence the name KBGit :-)

Features implemented

  • commits
  • branches
    • (create, list, delete)
    • detached heads
    • HEAD branch
  • checkout branches or commits
  • logging
  • push + pull
  • clone
  • remote (create, list, delete)
  • command line parser
  • store git state on disk

Planned work

  • git log
    • graphical (--graph)
    • patch (-p)
    • "less" implementation
  • git INDEX rather than scanning files
  • blob compression
  • diff'er to show changes to files
    • and select changes to index...

I will blog about the implementation on when the implementation has stabilized. Comments etc. are much welcommed.

For now the reading guide is to just read the Git.cs file. The main classes are the CommandLineHandling which is the facade of the implementation. The first class KBGit holds or redirects the main functionalities of Git. For now, just browse around with outset in those classes.