Sub topic 1
Commits form a graph
Prerequisites
This chapter assumes you can already run git init, git clone, git add, git commit, git status, and git log in a terminal. You need a repository with at least a few commits to try the commands. Later chapters use git switch, which needs Git 2.23 or later.
A commit is a snapshot with parent links
Each commit stores four things:
- A snapshot of the whole project at that moment.
- The author and committer, with timestamps.
- The commit message.
- The ID of its parent commit, or parents.
The ID is a hash of the commit's content, so it is unique to that exact content. You can see this with git cat-file:
git cat-file -p HEADThe output has a tree line for the snapshot, a parent line for the previous commit, then the author, committer, and message. The first commit in a repository has no parent line. A merge commit has two.
Why it is a graph
Links point from child to parent, never the other way. A commit cannot know which commits come after it, because those commits did not exist when its hash was computed. Commits never change, so a link never has to be updated.
Because each commit can have more than one child, history can fork. Because a merge commit has two parents, forks can join. The result is a directed acyclic graph (DAG). Directed means every link has a direction. Acyclic means you can never follow parent links and arrive back where you started.
In this graph, E is a merge commit with parents C and D. Commit B has two children, which is where the fork happened.
Reading the graph in your terminal
Git can draw the graph for you:
git log --oneline --graph --all--graph draws the lines between commits. --all includes every branch, not only the one you are on. Without --all, you only see history reachable from HEAD, so a fork can look like a straight line.
You can also address commits relative to each other:
git rev-parse HEAD
git rev-parse HEAD~1
git rev-parse HEAD^2~1 follows the first parent. ^2 selects the second parent, which only exists on a merge commit.
Why this matters for branches
Because history is a graph of immutable commits, Git never needs to copy files to start a new line of work. A new line of work only needs a name that marks one commit. The next chapter shows that this is all a branch is. Merging, rebasing, and cherry-picking are all operations on this graph.
Common mistakes
- Thinking a commit belongs to one branch. A commit is just a node. Any number of branches can reach it by following parent links.
- Reading
git logas the whole history. It follows parents from your current commit. Commits on other lines are hidden unless you add--all. - Expecting edited commits to stay the same. Amending or rebasing creates new commits with new IDs. The old ones stay in the graph until Git cleans them up.
Exercise
In the graph above, which commits does git log list when HEAD is at E, and which commit does HEAD^2 refer to?
Show answer
It lists E, D, C, B, and A, because it follows both parents of the merge commit E back to the root. HEAD^2 is C, the second parent of E.