Git's most perplexing paradigm

/ Jack Lot


Branches are just pointers to commits

Arguably Git’s most perplexing paradigm. We visualize branches as long chains of interconnected commits, so how can an entire branch be represented by a single, tiny, pointer?

The cheatsheet

Visualizing the commit tree

git log --graph --decorate --oneline

Draws a rudimentary ascii visualization of the commit chains with the branch labels attached. It’s not as good as a dedicated GUI, but it can help orient yourself.

What is actually inside a commit

A commit holds author information, a descriptive message, a unique identifier, and links to file versions so Git knows which files that commit changed. It also holds a pointer: a symbolic link to the commit directly before it.

This symbolic link is how commits form chains. Merge commits are the exception. They can have more than one parent, and thus more than one symbolic link.

Why one label per branch is enough

When you create a new commit, Git needs to know which branch it should append onto. Because each commit links to the previous commit in the chain, a single label on the most recent commit is enough to identify an entire chain of commits. Finding any particular commit requires walking the chain (thats an O(n) lookup time for my LeetCoders out there), but that still only a single pointer to identify a whole branch worth of commits.


TAGS: videos, git