The Hook (The "Byte-Sized" Intro)
Git history isn't a straight line — it's a graph. Every commit points to its parent(s), forming a Directed Acyclic Graph (DAG). Regular commits have one parent. Merge commits have two (or more). The root commit has zero. Understanding the graph is the key to understanding branches, merges, rebases, and why Git can do things other VCS tools can't.
📖 What is the Commit Graph?
The commit graph is the DAG (Directed Acyclic Graph) formed by commits pointing to their parent commits. Branches and tags are just named pointers into this graph.
Conceptual Clarity
Commit types by parent count:
| Type | Parents | When |
|---|---|---|
| Root commit | 0 | First commit in the repo |
| Regular commit | 1 | Normal development |
| Merge commit | 2+ | After git merge |
DAG properties:
- Directed: Each commit points TO its parent (backward in time)
- Acyclic: No commit can be its own ancestor (no loops)
- Graph: Branches create parallel paths that can merge
Branches and tags are just pointers:
main → E → D → C → B → A (root)
↗
feature → F → G
Real-Life Analogy
The commit graph is a family tree. Most people have two parents (merge commits). Some are only children (regular commits). The tree goes back to common ancestors but never loops back — no one is their own grandparent.
Visual Architecture
Why It Matters
- Branching: Branches are just pointers to nodes in the graph — creating one is O(1).
- Merging: Merge commits connect two graph paths.
- Rebase: Replays commits to create a linear graph (no merge commit).
- Reachability: Git can determine if commit A is an ancestor of commit B by walking the graph.
Code
# ─── Visualize the commit graph ───
git log --oneline --graph --all
# * a1b2c3d (HEAD -> main) Merge feature
# |\
# | * def4567 (feature) Add login
# | * 890abcd Add auth module
# * | 1234567 Fix bug
# |/
# * 5678901 Initial commit
# ─── See parent commits ───
git cat-file -p HEAD
# tree abc123
# parent def456 ← first parent (main line)
# parent 789abc ← second parent (merged branch)
# ─── Count parents ───
git rev-list --parents -1 HEAD
# Shows the commit SHA followed by parent SHAs
# ─── Find merge commits ───
git log --merges --oneline
# ─── Find the root commit ───
git rev-list --max-parents=0 HEADKey Takeaways
- Git history is a DAG — directed, acyclic, with branches and merges.
- Regular commits have 1 parent, merge commits have 2+, root has 0.
- Branches are pointers into the graph — not copies of code.
git log --graphvisualizes the DAG structure.
Interview Prep
-
Q: What is a DAG and how does Git use it? A: A DAG (Directed Acyclic Graph) is a graph where edges have direction and no cycles exist. In Git, commits are nodes and parent references are edges. This structure efficiently represents branching history, enables fast ancestor queries, and guarantees history consistency.
-
Q: How does a merge commit differ from a regular commit in the graph? A: A regular commit has one parent. A merge commit has two or more parents, representing the joining of two development lines. The first parent is typically the branch you merged INTO; additional parents are the branches merged FROM.
-
Q: Why are branches "cheap" in Git? A: Because a branch is just a 41-byte file containing a commit SHA — a pointer into the existing graph. Creating a branch doesn't copy any files or commits. It's an O(1) operation regardless of repository size.