μgit - Learn Git Internals by Building Git in Python
Tags: git, tutorial, python
Published: 2020-09-19 07:05:13

I'd like to show you an interactive tutorial I've built where we're going to implement Git in Python to learn more about how Git works on the inside.

This tutorial is different from most Git internals tutorials because we're not going to talk about Git only with words but also with code! We're going to write in Python as we go.

**This is not a tutorial on using Git**! To follow along I advise that you have working knowledge of Git. If you're a newcomer to Git, this tutorial is probably not the best place to start your Git journey. I suggest coming back here after you've used Git a bit and you're comfortable with making commits, branching, merging, pushing and pulling.

# Why learn Git internals?

For most tools that we use daily, we don't really care about their internals. We can use Firefox or Vim without understanding their inner workings.

At first you shouldn't care about Git internals either. You can use Git as a set of CLI commands that track code history. Run `git add`, `git commit` and `git push` all day long and you'll do fine, as long as you're a sole developer who just commits to one branch.

But once you start collaborating with multiple people on multiple branches and things like *rebase* or *force push* are getting involved, it's easy to become lost if you don't have a good mental model of Git internals.

From my experience with using Git myself and teaching others, a better way to improve your effectiveness with Git is by understanding how it works behind the scenes and not by learning more "advanced" Git commands. This understanding is what will allow you to solve the kind of problems that multi-user collaborative coding sometimes produce.

# Introducing: μgit

μgit (ugit) is a small implementation of a Git-like version control system (VCS). It's top goal is simplicity and educational value. ugit is implemented in small incremental steps, with each step explained in detail. Hopefully you will be able to read the small steps (explanation and code) and slowly build a complete picture of the internals.

ugit is not exactly Git, but it shares the important ideas of Git. ugit is way shorter and doesn't implement irrelevant features. For example, to reduce the complexity of ugit, ugit doesn't compress objects, doesn't save the mode of the files or doesn't save the time of a commit. But the important ideas, like commits, branches, the index, merges and remotes are all present and are very similar to Git. If you know ugit well you will be able to recognize the same ideas in Git.

This tutorial organized as a series of code changes, each change contains an explanation and the diff of the change. For example, you're now reading the first change, and you can see the code that we've added in this change as a diff on the other side. The code is an empty Python application that prints "hello world".

# Let's begin

The tutorial is an interactive web experience that I've been working on for a long time. Because of the interactivity, I can't just post it here as a blog post. So please visit the tutorial here:

[Go to μgit tutorial](https://www.leshenko.net/p/ugit/)

I hope you enjoy it and I'd be happy to answer any questions!