Compare Branches and Commits with git diff (Visual Guide)

In this video, we will understand the git diff command properly.
To make it visual, we first create a new branch and add two commits there. Then we will compare:

  • Working directory vs staging area
  • Branch vs branch
  • Commit vs commit
  • Filename-only and status output

Fast setup: new branch + two commits

We already have history on main:

git log --oneline
# 9672151 Second commit
# af9916f First commit

Create and switch to a feature branch:

git switch -c feature_chat

Diagram — switched to new branch (HEAD now on feature_chat; HEAD points to commit):


Commit 1 on feature_chat: add chat.txt

echo "new file added for chat feature" > chat.txt
git add chat.txt
git commit -m "Add chat.txt for chat feature"

Diagram — first feature commit (HEAD and feature_chat both point to new commit):


Commit 2 on feature_chat: modify hello.txt

echo "chat module integrated" >> hello.txt
git add hello.txt
git commit -m "Chat module integrated"

Diagram — second feature commit (HEAD→commit, feature_chat→same commit):


Grab commit IDs quickly when needed:

git log --oneline
# 8f3d0e1 Chat module integrated
# 71c2a9a Add chat.txt for chat feature
# 9672151 Second commit
# af9916f First commit

What git diff compares by default

git diff

By default, this shows working directory vs staging area. If nothing is modified (clean working tree), it prints nothing.

To compare staged files vs last commit:

git diff --staged

Compare branch to branch

We want to see how feature_chat differs from main:

git diff main..feature_chat

What you will observe in our setup:

  • chat.txt exists on feature_chat but not on main.
  • hello.txt has extra lines on feature_chat.

Only filenames changed:

git diff --name-only main..feature_chat
# chat.txt
# hello.txt

Filenames with status letters (A/M/D):

git diff --name-status main..feature_chat
# A	chat.txt
# M	hello.txt

Tip: If you pass one branch name (e.g., git diff feature_chat) while you are on main, Git compares the current branch (main) to the given one (feature_chat).


Compare commit to commit

Use commit IDs from git log --oneline:

git diff 71c2a9a..8f3d0e1

This shows exactly what changed between those two commits on the feature branch.


Visual: what we just compared



Summary

  • git diff (no args) → working directory vs staging area.
  • git diff --stagedstaging area vs last commit.
  • git diff main..feature_chatbranch vs branch (what changed on feature compared to main).
  • git diff <commit1>..<commit2>commit vs commit.
  • --name-only shows changed filenames; --name-status shows A/M/D with filenames.

Next, we will practice merging and visualize exactly how the pointers move.


Keep Learning 🚀

👉 Subscribe to Learning Ocean – Subscribers get coupon codes for my courses, early access to blogs and courses, and even exclusive YouTube videos.

👉 My YouTube Channel – More videos, more fun, and lots of learning!

👉 📺 Watch this topic in video form

Stay curious, keep coding, and let’s make learning fun together! 🎉