Sub topic 1
Commits, branches, and the commit graph
Every GitHub merge method changes the same thing: the commit graph of the base branch. This chapter gives you the model of that graph, so the later chapters on merge commits, squash merges, and rebase merges can be read as changes to it.
Prerequisites
This chapter assumes you can:
- Commit, branch, push, and pull with Git.
- Open and review a pull request on GitHub.
- Read basic
git logoutput.
Commits are snapshots that point backward
A commit stores a snapshot of your files, an author, a message, and a pointer to its parent commit. The commit is identified by a hash of that content. Most commits have one parent. A merge commit has two. The first commit in a repository has none.
A commit never changes after it is created. Commands that seem to edit a commit, such as git commit --amend and git rebase, create new commits with new hashes. This matters later, because a rebase merge works exactly this way.
Branches are movable labels
A branch is a name that points at one commit. When you commit on a branch, Git creates a new commit whose parent is the old tip, then moves the label forward. Creating a branch copies no files. It adds a second label.
HEAD is a pointer to the branch you have checked out. Remote branches such as origin/main are your local record of where the remote labels were at the last fetch.
The graph
Following parent pointers from any commit gives you its history. Together, all commits and their parent links form a directed acyclic graph. Arrows point from child to parent, and there are no cycles.
A branch diverges when two commits share the same parent. That is the normal state of a pull request: the base branch keeps moving while the feature branch adds its own commits.
Here feature has two commits that main lacks (C and D). main has one commit that feature lacks (E). The common ancestor is B. Git uses that ancestor when it combines the two lines of work.
Seeing the graph yourself
This script builds the graph above in a scratch repository. It uses empty commits, so you do not need any files.
git init -b main graph-demo
cd graph-demo
git commit --allow-empty -m "A"
git commit --allow-empty -m "B"
git switch -c feature
git commit --allow-empty -m "C"
git commit --allow-empty -m "D"
git switch main
git commit --allow-empty -m "E"
git log --oneline --graph --decorate --allThe output looks like this. Your hashes will differ.
* 3f2a1c9 (HEAD -> main) E
| * 8b7d4e2 (feature) D
| * c41e0aa C
|/
* 91d6b35 B
* e07f2c8 AThe |/ line is the point where the two lines join at B. The --decorate flag shows which labels point at which commits. You will use this command throughout the tutorial to see which shape a merge leaves on main.
What a pull request is in graph terms
A pull request is a request to bring the commits that are on the head branch (feature) but not on the base branch (main) into the base. GitHub shows those commits and their combined diff. Merging means choosing how they appear on main:
- Keep C and D as they are and join the lines with a new commit.
- Replace C and D with one new commit.
- Replay C and D as new commits on top of E.
The next chapter introduces these three outcomes. Each one produces a different graph, and the differences decide how easy it is to read, bisect, and revert history.
Exercise
In the graph above, which commits are on feature but not on main, and which commit would Git use as the common ancestor?
Show answer
C and D are on feature but not on main. The common ancestor is B, the most recent commit reachable from both branches. E is on main only.
Exercise
You run git commit --amend on the latest commit of your feature branch. Does the old commit get edited in place?
Show answer
No. Git creates a new commit with a new hash and moves the branch label to it. The old commit still exists until it is garbage collected, but the branch no longer points to it.