Git Academy

Advanced · 7 min read

How Git works inside

Blobs, trees, commits and refs: once you see them, nothing in Git is magic anymore.

Four kinds of objects

A blob is a file's content. A tree is a folder (names → blobs and trees). A commit points to a tree, its parent(s), an author and a message. A tag points to a commit. Each one is identified by the hash of its content.

Safe
$ git cat-file -p HEAD
$ git cat-file -p HEAD^{tree}

Branches are just files

A branch is a small file containing a commit hash. HEAD says which branch you're on. That's why creating a branch is instant.

Safe
$ cat .git/HEAD
$ git rev-parse main

Why almost nothing is lost

Objects are never modified, only added. A reset or a deleted branch only moves or removes a name; the objects stay until garbage collection, and the reflog remembers where the names were.

Safe
$ git reflog
$ git fsck --unreachable