~/portfolio

~/blog / building-nit-my-own-git-in-rust

Building Nit: My Own Git in Rust

Building Nit: My Own Git in Rust

I wrote a small Git clone in Rust. I called it Nit. It was one of the best ways to understand how Git really works.

Why I Built It

You use Git every day, but most people only know a few commands. I wanted to know what happens inside when I run git commit. The best way was to build it myself.

How Git Works Inside

Git stores everything as objects in a folder called .git/objects. There are three main types:

  • blobs: the file content
  • trees: folders that point to blobs and other trees
  • commits: a snapshot with a message, author, and parent

Each object has a name based on its content, using SHA-1. Git also compresses objects with zlib. My Rust code did the same with the flate2 crate.

The Index

The index (or staging area) is a binary file. It lists the files you added with git add. I had to implement its exact format, so that real Git tools like git status could still read my repositories.

Commands I Implemented

  • nit init
  • nit add
  • nit status
  • nit commit
  • nit clone (local repositories)
  • and more plumbing commands

Repositories I create with Nit can be opened with normal Git. That was a great test.

What I Learned

  • hashing and content-based naming
  • binary formats and parsing
  • compression with zlib
  • how commits link together as a graph

Hard Parts

  • packfiles with delta objects are still not complete
  • .gitignore is not implemented yet
  • remote clone over HTTP is incomplete

These are the parts I want to do next.

Final Thoughts

Building Nit made me a better Git user and a better programmer. If you want to understand a tool deeply, try to build a small version of it.

Repository: GitHub - m-elhabib-dev/nit