[!IMPORTANT] The DAG (Directed Acyclic Graph) is the single most important data structure in Git. It is the mathematical foundation of everything.
What is a DAG?
- Directed: The edges (arrows) have a direction. In Git, a commit points to its parent.
- Acyclic: There are no loops. You cannot go back in time to a child commit from a parent.
- Graph: A collection of nodes (commits) and edges (parent pointers).
The Anatomy of History
Every commit stores a pointer to its parent. This forms a chain.
Commit C (Child) -> Commit B (Parent) -> Commit A (Grandparent)
Because the parent’s hash is part of the child’s data (in the commit object), you cannot change a parent without changing the child.
Why History is Immutable
If you try to change Commit A:
- Commit A gets a new hash (SHA-1).
- Commit B points to the old A. To point to the new A, B must change.
- If B changes, it gets a new hash.
- Commit C must now update to point to the new B.
This “avalanche effect” ensures that history is tamper-proof.
Interactive: DAG Builder
Click “Commit” to create a new commit node. Click “Branch” to create a new branch pointer. Visualize how history grows.
Branches are Cheap
A common misconception is that a branch is a copy of your files. It is not.
A Branch is simply a text file containing the 40-character SHA-1 hash of a commit.
- Creating a branch takes 0 bytes of disk space (almost).
- It is just a movable sticker.
cat .git/refs/heads/main
# e4f5g6h7...
When you commit, the main branch pointer simply moves forward to the new commit. This is why branching in Git is instantaneous.