Skip to content

Building Git

Let's tackle it!

1. Introduction

The book is predicated on a neat idea: let's write our own version of git and base our implementation on observing git, logging system calls and (where we must) reading the source code. We call it Jit and it's written in Ruby.

gem install jit

2. Getting to know git

What's the bare minimum needed to commit something?

https://git-scm.com/docs/git-reflog

.git/HEAD contains a reference to the current commit.

# cat .git/HEAD
ref: refs/heads/main

Git shortenings:

  • reflog - reference log
  • symref - symbolic reference

πŸ’‘ Ah, .git/info/exclude is presented as a possible alternative to .gitignore. While .gitignore is part of the source tree, the contents of .git/info/exclude are local to that copy of the repository.

On to more good stuff .git/objects:

When an object is first added to the database, it's stored in its own file as a loose object. On certain events, files are assembled into a pack. The index files describe where to find a particular item in a pack. This can be triggered with git gc.

TL;DR

Do:

git init
tree .

Inspect the contents.

A simple commit

A root commit is a commit with no parents. A ref can point to a commit (like HEAD) or a branch...for example. That's not an exhaustive list. The reflog command uses .git/logs. Ah, the conten of .git/refs/heads/main will tell us which commit is at the tip of the main branch.

Storing objects


πŸ“š Further Reading