Sub topic 1
Commits form a graph
This chapter builds the model that every later branch command depends on. A Git repository stores history as a graph of commits, and branches, merges, and rebases are all operations on that graph.
Prerequisites
This chapter assumes you can do the following:
- Run
git init,git clone,git add,git commit,git status, andgit login a terminal. - Read a short commit history in
git logoutput. - Use a repository you can experiment in. Git 2.23 or later is needed for
git switch, which later chapters use.
You do not need to know anything about branches yet.
What a commit contains
A commit is a snapshot of your whole project plus some metadata. It is not a diff. Git can compute a diff between any two commits, but what it stores is the state of the files at the moment you committed.
Each commit records:
- A pointer to a tree object, which describes the directory layout and file contents at that moment.
- The author and committer names, email addresses, and timestamps.
- The commit message.
- Zero or more pointers to parent commits.
Git identifies each commit by a hash of this content, usually written as a 40-character hexadecimal string such as a1b2c3d.... Because the parent pointer is part of the hashed content, changing a commit's parent, message, or files produces a different hash. This is why Git commits are described as immutable. When you "edit" a commit with git commit --amend or git rebase, Git creates a new commit and leaves the old one in place.
You can look at the raw content of a commit with git cat-file:
git cat-file -p HEADThe output has a tree line, one or more parent lines, author and committer lines, and the message. The first commit in a repository has no parent line.
Parents make a graph
The parent pointer is the only link between commits. Each commit points back to the commit or commits it was built on. It never points forward, because a commit cannot know about commits created after it.
Three cases cover every commit you will meet:
| Kind of commit | Number of parents | How it arises |
|---|---|---|
| Root commit | 0 | The first commit in a repository |
| Ordinary commit | 1 | A normal `git commit` on top of the current commit |
| Merge commit | 2 or more | A merge that joins two lines of history |
Because commits point only to their parents, the whole history forms a directed acyclic graph, often shortened to DAG. Directed means each link has a direction, from child to parent. Acyclic means you can never follow parent links and arrive back where you started. A commit's hash depends on its parents' hashes, so a cycle could not be built.
A repository with a single line of work is a degenerate graph that looks like a chain:
Arrows run from child to parent, so they point toward older commits. Many diagrams draw history left to right with arrows pointing left for this reason.
Divergence and merging create branching shapes
The graph stops being a chain when two commits share the same parent. That happens whenever you commit on top of a commit that already has a child, and it is the structure that branches make possible.
In this graph, D and E both have C as their parent. Neither one is an ancestor of the other, so the history has diverged. Commit F has two parents, which makes it a merge commit. It joins the two lines back together.
Terms you will use throughout this tutorial:
- Ancestor: any commit you can reach by following parent links.
A,B, andCare ancestors ofF. - Descendant: the reverse.
Fis a descendant ofA. - Reachable: commit X is reachable from commit Y if X is Y or an ancestor of Y.
- Common ancestor: a commit reachable from two different commits.
Cis a common ancestor ofDandE. - Merge base: the best common ancestor, meaning the one closest to both tips. Git uses it as the starting point when it merges.
You can ask Git for the merge base directly:
git merge-base <commit-a> <commit-b>Later chapters on three-way merges and rebasing rely on this idea, so it is worth keeping in mind now.
Looking at the graph
git log shows the history reachable from your current commit. Two options make the graph shape visible:
git log --oneline --graph --decorate--onelineshows one abbreviated hash and the message subject per commit.--graphdraws ASCII lines between commits to show parent links and merges.--decoratelabels commits with the branch names and tags that point at them.
To see every line of history in the repository rather than only what is reachable from HEAD, add --all:
git log --oneline --graph --decorate --allFor a merged history like the one in the diagram above, the output looks similar to this. The hashes and branch names are examples, and yours will differ:
* 9f3c2ab (HEAD -> main) Merge branch 'feature'
|\
| * 4d1e7c0 (feature) Add search box
* | 8b02a55 Fix typo in footer
|/
* 3ac9f10 Update README
* 1e5d6b2 Initial commitRead it from the top. The newest commit is at the top, and each * is a commit. The |\ line shows the top commit has two parents, and the |/ line shows where the two lines meet again.
Which commits `git log` shows
git log walks backward from a starting commit, following every parent link, and prints what it finds. This has some consequences that surprise people:
- The order is not strictly by date. With merges in the graph, Git shows children before parents but does not promise a timestamp order unless you ask for one with
--date-orderor--author-date-order. - Commits that are not reachable from your starting point do not appear. A commit on a different line of work is invisible to a plain
git loguntil you name it or use--all. - Commits are never removed from the graph by being unreachable. They remain in the object database until Git's garbage collection deletes them, which by default happens only after they have been unreachable for a long time. Later, the reflog chapter relies on this.
A useful form is the range syntax A..B, which means "commits reachable from B but not from A". It answers the question of what B has that A lacks:
git log --oneline main..featureThis lists the commits on feature that main does not contain. You will use the same notation later with remote-tracking branches, as in main..origin/main.
Common mistakes
- Thinking a commit is a diff. Git stores snapshots. Diffs shown by
git showandgit log -pare computed on demand by comparing a commit to its parent. - Thinking history is a straight line. Once you merge, one commit has two parents, and "the previous commit" is ambiguous. Use
HEAD~1orHEAD^1for the first parent andHEAD^2for the second parent of a merge. - Thinking a commit belongs to one branch. A commit is just a node in the graph. Several branches can reach the same commit. The next chapter shows that a branch is only a label attached to one commit.
- Expecting amended or rebased commits to be edited in place. They are replaced by new commits with new hashes, and the old ones stay in the graph until they are cleaned up.
Summary
- A commit is a snapshot with metadata and zero or more parent pointers.
- Parent links point from child to older commit, and they form a directed acyclic graph.
- A root commit has no parents, an ordinary commit has one, and a merge commit has two or more.
- Two commits that share a parent mean the history has diverged. The merge base is the nearest common ancestor.
git log --oneline --graph --decorate --allshows the shape of the graph, andA..Bselects commits reachable from B but not from A.
Exercise
In a repository, commit X has parent P, and commit Y also has parent P. Commit M has the two parents X and Y. (1) How many parents does P have if it is the first commit ever made? (2) What does git log --oneline X..M list? (3) What does git merge-base X Y print?
Show answer
(1) None. A commit with no parents is a root commit. (2) It lists the commits reachable from M but not from X. M itself and Y qualify. P and anything older is reachable from X, so those are excluded. (3) It prints the hash of P, the nearest common ancestor of X and Y.
Exercise
A teammate says that after running git commit --amend, the old commit is gone from the repository. Is that accurate, and why or why not?
Show answer
Not exactly. A commit's hash includes its content and its parent hashes, so an amended commit is a new object with a new hash. The original commit is still stored in the object database. It is just no longer reachable from the branch. It stays there until garbage collection removes it, which is why it can often be recovered with the reflog.