Sub topic 1
Commits, branches, and the commit graph
GitHub's three merge methods differ in one thing: which commits they add to the base branch, and whether those commits are new objects or existing ones. You can't judge that without a precise picture of what a commit is and how branches point at commits. This chapter builds that picture. Later chapters rely on it.
Prerequisites
This chapter assumes you can do the following:
- Run basic Git commands:
git add,git commit,git switch,git push, andgit pull. - Open and review a pull request on GitHub.
- Read the output of
git log --oneline.
You don't need to know how merging, squashing, or rebasing works internally. This chapter covers the concepts those chapters depend on.
What a commit is
A commit is a snapshot of your whole project, plus metadata. It is not a diff. Git stores:
- A pointer to the full tree of files at that moment.
- The author and committer names, emails, and timestamps.
- The commit message.
- The identifiers of the commit's parents.
Git identifies each commit by a SHA, a hash computed from all of that content. The parent identifiers are part of the content. This has a consequence that explains most of the chapters ahead: if you change anything about a commit, including its parent, you get a different SHA, which means a different commit.
- A commit with one parent is an ordinary commit.
- A commit with no parent is a root commit, the first one in a repository.
- A commit with two parents is a merge commit.
Tools like git show display a diff, but Git computes it on demand by comparing a commit's snapshot with its parent's snapshot.
Branches are movable labels
A branch is a name that points at one commit. It's a small file containing a SHA. When you commit on a branch, Git creates the new commit with the current tip as its parent, then moves the branch label to the new commit. Nothing else about the branch is stored.
| Name | What it is | Moves when |
|---|---|---|
| `main` (a branch) | Label on one commit | You commit while it is checked out, or a merge or push updates it |
| `HEAD` | Pointer to the branch you have checked out | You run `git switch` |
| `origin/main` | Your local record of the remote branch | You run `git fetch` or `git pull` |
| A tag | Label on one commit | It doesn't move after creation |
Because a branch is only a label, creating one is nearly free. Deleting one deletes the label, not the commits. Commits that no label can reach become candidates for garbage collection. This is why deleting a head branch after merging a pull request is safe when the commits are reachable from the base branch. It's also why the branch name alone tells you little about what the merge did.
The commit graph
Every commit points back to its parents, and no commit can be its own ancestor. The result is a directed acyclic graph, usually called the commit graph. Arrows point from child to parent, but diagrams usually draw time flowing left to right.
Two terms matter for the rest of the tutorial:
- Reachable: commit X is reachable from a branch if you can get to X by following parent links from the branch's tip. Reachable commits are "in" the branch.
- Merge base: the most recent commit reachable from both of two branches. It's the point where they diverged.
Here is a small history. Both main and feature start from commit B. Then feature gets two commits, and main gets one.
Commit B is the merge base. The pull request for feature would show C and D as its changes, because they are reachable from feature but not from main.
Build the example yourself
Run this script in an empty directory to reproduce the graph above. The git config lines make it run even if you have no global identity set.
git init -b main demo
cd demo
git config user.name "Demo User"
git config user.email "[email protected]"
echo "one" > notes.txt
git add notes.txt
git commit -m "A: add notes"
echo "two" >> notes.txt
git commit -am "B: second line"
git switch -c feature
echo "three" > feature.txt
git add feature.txt
git commit -m "C: add feature file"
echo "four" >> feature.txt
git commit -am "D: extend feature"
git switch main
echo "five" > other.txt
git add other.txt
git commit -m "E: add other file"
git log --oneline --graph --decorate --allThe last command prints something like this. Your SHAs will differ, and commits made in the same second can appear in a different order.
* 3f2a9c1 (HEAD -> main) E: add other file
| * 8d41b7e (feature) D: extend feature
| * c07e5a2 C: add feature file
|/
* 5b9d0f4 B: second line
* a1c3e68 A: add notesRead the output this way:
- Each
*is a commit. - The
|and/characters show lines of history that split and rejoin. - The names in parentheses are labels.
HEAD -> mainmeans you havemainchecked out and it points at E.
Asking the graph questions
Git has two range notations, and both come up when you reason about pull requests.
A..Bmeans commits reachable from B but not from A.A...B(three dots) means commits reachable from either, but not both. It is the symmetric difference.
git merge-base main feature
git log --oneline main..feature
git log --oneline feature..mainThe first command prints the SHA of commit B. The second lists D and C, which is the content of the pull request. The third lists E, the work on main that feature doesn't have yet.
GitHub's "Files changed" tab for a pull request shows the changes between the merge base and the head branch. It is not a comparison of the two branch tips. That's why changes made on main after you branched don't show up as "removed" in your pull request.
What a merge commit looks like
Now merge feature into main with Git's default behavior. This is what GitHub's "Create a merge commit" button does.
git merge feature -m "Merge branch 'feature'"
git log --oneline --graph --decorate
git rev-parse HEAD^1 HEAD^2HEAD^1 is the first parent, the commit main pointed at before the merge (E). HEAD^2 is the second parent, the tip of the merged branch (D). The log now shows the graph rejoining:
* 9e6b2d3 (HEAD -> main) Merge branch 'feature'
|\
| * 8d41b7e (feature) D: extend feature
| * c07e5a2 C: add feature file
* | 3f2a9c1 E: add other file
|/
* 5b9d0f4 B: second line
* a1c3e68 A: add notesTwo properties of this result will come back repeatedly:
- No existing commit changed. C, D, and E keep their SHAs. The merge only added one commit with two parents.
- The first parent is the base branch's side. Following only first parents from
maingives you the sequence of merges and direct commits onmain. You can see this withgit log --first-parent --oneline.
Why identity matters for the other two methods
Because the SHA depends on the parents, you can't "move" a commit. You can only create a new commit with the same changes and a different parent. Two of GitHub's merge methods do exactly that:
- Rebase and merge replays each commit of the branch on top of the base branch. Each replayed commit is a new commit with a new SHA.
- Squash and merge combines the whole branch into one new commit on the base branch. That commit has a single parent and no link back to the branch's commits.
After either method, the original commits (C and D in our example) still exist on the head branch, but the base branch doesn't contain them. Git sees the base branch's copies as unrelated commits that happen to carry similar changes. This explains the problems covered later:
- Why a branch started from a squashed branch produces duplicate conflicts.
- Why reverting works differently for each method.
- Why
git branch --mergeddoesn't list a branch that was squash-merged.
Summary of the model
- A commit is a snapshot with parent links, identified by a SHA that includes those links.
- A branch is a label on one commit.
HEADpoints at the branch you're on. - History is a graph. A branch contains every commit reachable from its tip.
- The merge base is where two branches diverged, and a pull request shows changes since that point.
- A merge commit has two parents and leaves existing commits untouched. Rebasing and squashing create new commits with new SHAs.
Exercise
In the example graph, main points at E and feature points at D. Which commits does git log --oneline main..feature list, and what is the merge base of the two branches?
Show answer
It lists D and C, the commits reachable from feature but not from main. The merge base is B, the most recent commit reachable from both branches.
Exercise
You rebase a commit onto a different parent without changing its files or message. Does it keep the same SHA? Explain why or why not.
Show answer
No. The parent identifier is part of the data that Git hashes to produce the SHA. A different parent produces a different hash, so the result is a new commit even though the file changes and message are identical.
Exercise
After merging feature with a merge commit, what do HEAD^1 and HEAD^2 refer to?
Show answer
HEAD^1 is the first parent, the previous tip of the base branch (E in the example). HEAD^2 is the second parent, the tip of the merged branch (D in the example).