Lesson 4 — Comparing Branches¶
1. Learning objectives¶
By the end of this lesson, you should understand:
- How to compare two local branches.
- How to compare commits.
- The difference between two-dot and three-dot comparisons.
- How to inspect files changed between branches.
- How to identify commits ahead or behind.
- How to use GitHub Compare.
- How branch comparisons support pull request reviews.
2. Why compare branches?¶
Before merging a branch, developers need to understand exactly what changed.
Branch comparisons help answer:
- Which files were modified?
- What lines were added?
- What lines were removed?
- Which commits exist on one branch but not the other?
- Is the feature branch ahead of
main? - Does the branch contain unexpected changes?
This is an important step before creating or reviewing a pull request.
3. Update remote information¶
Before comparing local branches with GitHub, fetch the latest remote information:
This updates remote-tracking branches without automatically merging them into your current branch.
List branches:
4. Compare two local branches¶
Compare the tips of two branches:
This displays the difference between the two specified branch tips.
Show only changed filenames:
Show filenames and change status:
Show a summary:
Inspect one file:
5. Understand two-dot and three-dot comparisons¶
Two-dot comparison¶
This compares the two endpoint trees. It is equivalent to comparing the tree at main with the tree at the feature branch.
Three-dot comparison¶
This compares the merge base of the two branches with the feature branch.
The merge base is their best common ancestor commit.
Three-dot comparison is useful when you want to inspect what the feature branch has introduced since it diverged from main.
This is particularly useful for understanding the changes a pull request proposes.
6. Compare commits¶
Compare two commit snapshots:
Compare the previous commit with the current commit:
Show only the changed filenames:
Show a summary:
Inspect the changes introduced by a specific commit:
7. Understand ahead and behind commits¶
You can determine how many commits each branch has that the other does not.
Example output:
Interpretation:
- First number: commits reachable from
mainbut not from the feature branch. - Second number: commits reachable from the feature branch but not from
main.
Therefore:
mainhas 2 unique commits.- The feature branch has 3 unique commits.
8. Inspect commits unique to a branch¶
Show commits in the feature branch that are not in main:
Show commits in main that are not in the feature branch:
These commands help explain why a branch is ahead or behind.
9. Compare branches on GitHub¶
GitHub provides a visual comparison interface.
Open your repository and navigate to the Compare page.
You can also use:
Select:
- Base: the branch you want to merge into.
- Compare: the branch containing your changes.
For example:
GitHub displays:
Commits¶
The commits included in the comparison.
Files changed¶
The files modified between the branches.
Diff view¶
Green lines generally represent additions.
Red lines generally represent removals.
You can inspect the changes before creating a pull request.
10. Practical exercise¶
Create a documentation branch from updated main:
Create:
Add sections explaining:
- Why branch comparison is useful.
- How to compare branches using
git diff. - The difference between two-dot and three-dot comparisons.
- How to inspect changed filenames.
- How to compare commits.
- How to use GitHub Compare.
Commit:
Push:
11. Compare locally¶
Fetch the latest remote references:
Compare main with the new documentation branch:
List changed files:
Inspect the commit difference:
Inspect ahead/behind counts:
12. Compare on GitHub¶
Open:
Select:
Review:
- Commit list.
- Changed files.
- Added lines.
- Removed lines.
- Unexpected modifications.
Do not merge until you understand the changes.
13. Important commands reference¶
| Command | Purpose |
|---|---|
git fetch origin |
Update remote-tracking references |
git branch -a |
List local and remote-tracking branches |
git diff A..B |
Compare two branch tips |
git diff A...B |
Compare merge base with branch B |
git diff --name-only A..B |
List changed filenames |
git diff --name-status A..B |
List filenames and change status |
git diff --stat A..B |
Show change statistics |
git diff A B |
Compare two commits |
git show HASH |
Inspect a commit |
git log --oneline A..B |
Show commits in B not in A |
git rev-list --left-right --count A...B |
Count unique commits on each side |
14. Completion checklist¶
- [ ] Fetch remote information.
- [ ] Compare two local branches.
- [ ] Use two-dot comparison.
- [ ] Use three-dot comparison.
- [ ] List changed filenames.
- [ ] Inspect branch differences.
- [ ] Compare two commits.
- [ ] Inspect commits unique to a branch.
- [ ] Understand ahead and behind counts.
- [ ] Use GitHub Compare.
- [ ] Review the feature branch before merging.
15. What comes next?¶
Lesson 5 covers Git tags, semantic versioning, and GitHub Releases.
You will learn how to identify important project versions, create annotated tags, push tags, and publish releases on GitHub.