Sub topic 1
Commits, Parents, and the Commit Graph
Prerequisites
This chapter assumes you can already do the following:
- Run
git add,git commit,git branch,git switch,git push, andgit pull. - Read basic
git logoutput. - Recognize remote branches such as
origin/main.
You do not need any knowledge of rebase yet. This chapter builds the model that the rest of the tutorial relies on.
What a commit stores
A commit is a snapshot of your project plus some metadata. Git stores these items:
- A pointer to the full project tree at that moment.
- The author, the committer, and the timestamps.
- The commit message.
- The hash of each parent commit.
Git identifies every commit by a hash, a long hexadecimal ID computed from the commit's contents. The hash covers the tree, the message, the metadata, and the parent hashes. Change any of those and you get a different hash.
This matters for rebase. A commit cannot be edited in place. Anything that "changes" a commit actually creates a new one.
Parents form the graph
Each commit records the hash of its parent, the commit it was built on. A normal commit has one parent. The first commit in a repository has none. A merge commit has two or more.
Because each commit points back to its parents, the history forms a directed acyclic graph. "Directed" means the links go one way, from child to parent. "Acyclic" means you can never follow parent links and return to where you started.
In this diagram, the arrows point from child to parent. Commits C and D are on one line of work, and E and F are on another. Both lines share B as their common ancestor.
Reading the graph with git log
You can see the graph from the terminal:
git log --oneline --graph --allThe --graph option draws the parent links with ASCII lines. The --all option includes every branch, not only the one you are on. A small repository might print this:
* 5c1d9e2 (HEAD -> feature) Add session tests
* 8b7a4f0 Add session handling
| * 3e6f1a7 (origin/main, main) Update README
| * c92d0b4 Fix build script
|/
* 1a2b3c4 Initial project layoutEach * is a commit. The |/ line marks where the two lines of work split. Both lines descend from 1a2b3c4.
Inspecting parents directly
You can ask Git for a commit's parents:
git log -1 --format="%h parent(s): %p" HEAD
git rev-parse HEAD^The %p placeholder prints the abbreviated parent hashes. The HEAD^ syntax names the first parent of HEAD, and HEAD~2 names the grandparent. A merge commit prints two hashes in %p.
Why this matters for rebase
Two points from this chapter carry through the rest of the tutorial:
- Commits are immutable. Rebase does not move commits. It builds copies with different parents.
- History is a graph defined by parent links. Changing a commit's parent changes its identity, and the identity of all its descendants.
The next chapter covers branches and HEAD, the pointers that label positions in this graph.
Exercise
A repository has the linear history A <- B <- C, where C is the newest commit. You amend B with a new message, which produces B'. What happens to C?
Show answer
C keeps its original hash only if its parent is unchanged, but its parent is now B'. Git cannot alter C, so keeping the new history requires a new commit C' with parent B'. The original C still exists and points to the old B. Any change to a commit forces new hashes for all of its descendants.