AI Hints
Home›Git & GitHub Tutorial›Comparing Branches
Git & GitHub · Lesson 51 of 144

Comparing Branches

What is on each branch?.

⏱ 2 min read▶ Video at 55:02🎓 Free
Watch this lesson in the Git & GitHub courseWatch on YouTube ↗Full Git & GitHub course

Before you merge a branch, it's smart to check what's on it. Which commits will come in? Which files will change? Git can compare branches with the same commands you know, plus two dots.

Comparing Branches
From the video: Comparing Branches
  • Check before you merge
  • Which commits? Which files?
  • Compare with two dots

Commits on one branch only

Git log main, dot dot, dark, means commits that are on dark, but not on main. Two commits, the dark theme work. Flip it around, dark dot dot main, for commits on main, but not on dark. One commit, say hello on about page.

git
git log --oneline main..dark
0c2baaa (dark) Load dark theme
8bf46c4 Add dark theme
git log --oneline dark..main
1b2d030 (HEAD -> main) Say hello on about page

Files that would change

To see what a merge would bring in, use git diff with three dots. Main, dot dot dot, dark. Three dots means compare dark with the point where it split from main. So it shows only the dark branch's own changes. Dash dash stat keeps it short.

git
git diff --stat main...dark
 dark.css   | 1 +
 index.html | 1 +
 2 files changed, 2 insertions(+)
git diff main...dark
diff --git a/dark.css b/dark.css
new file mode 100644
index 0000000..dc18ed3
--- /dev/null
+++ b/dark.css
@@ -0,0 +1 @@
+body { background: #111; color: #eee; }
diff --git a/index.html b/index.html
index 93dd595..faba939 100644
--- a/index.html
+++ b/index.html
@@ -1,3 +1,4 @@
 <h1>My Site</h1>
 <p>Welcome!</p>
 <a href="about.html">About</a>
+<link rel="stylesheet" href="dark.css">

Two dots and three dots

Here's the cheat sheet. Log with two dots lists commits on the second branch, but not the first. Diff with three dots shows what the second branch changed, since it split off. Use them before every merge, and before reviewing a pull request.

CommandShows
git log main..darkcommits only on dark
git log dark..maincommits only on main
git diff main...darkdark's own changes
--statjust a summary

Summary

Git log with two dots shows commits only on one branch. Git diff with three dots shows a branch's own changes. Next, practising branches with a tiny experiment.

Key takeaways
  • log a..b
  • diff a...b
  • Check before merging
Next up: Practice: An Experiment — try, then throw away