The Directed Acyclic Graph
The Directed Acyclic Graph
After this page you can draw a tiny Git history as a graph and say what a branch is.
Snapshots with parents
A commit is a snapshot of the whole project at one instant, plus metadata (author, message, time) and a pointer to the commit that came before it — its parent.
Two commits in a row:
A ← B
B’s parent is A. Time flows left to right in this book’s diagrams; the parent pointer points backward, toward history.
A chain of commits is a directed acyclic graph (DAG): directed because each commit points at parents, acyclic because a commit cannot be its own ancestor.
A branch is a name
A branch is a movable name for one commit. main is a file that contains a hash. When you commit, Git writes a new snapshot whose parent is the current tip, then slides the name forward.
A ← B ← C main
main now names C. A and B are still there. Nothing was overwritten.
A second line of work
Start a branch named feature while main is at C, then add two commits:
A ← B ← C main
↑
D ← E feature
C has two children. Git is fine with that. A merge commit has two parents:
A ← B ← C ← M main
↑ ↑
D ← E feature
M ties the two lines together. Rebase (chapter 9) copies D and E onto a new parent instead. New hashes, similar diffs. You will do that after first-repository muscle memory is in place.
HEAD
HEAD is “where I am.” Most of the time it points at a branch name, and the branch name points at a commit:
HEAD → refs/heads/main → C
Commands that “move HEAD” are moving that name, or (when HEAD is detached) pointing HEAD at a raw commit hash. Detached HEAD shows up when you check out a tag or a hash. New commits on a detached HEAD are easy to lose when you switch away. Create a branch before you leave, or find them later with the reflog (chapter 17).
git log --oneline --graph --all
git rev-parse HEAD main
git statusgit status says HEAD detached at … when it is.
Look at the graph
Once you have a repository (next chapter’s neighbor, chapter 3):
git log --oneline --graph --decorate --all--decorate prints the names (main, HEAD, tags) next to the commits they point at. The graph is the truth. Commands add objects and move names. If you hold the graph, reset, rebase, and reflog stop feeling like magic.