How can I see the differences between two branches?

Learn how can i see the differences between two branches? with practical examples, diagrams, and best practices. Covers git, git-diff development techniques with visual explanations.

Mastering Git: How to See Differences Between Branches

Hero image for How can I see the differences between two branches?

Learn essential Git commands to compare changes between any two branches, understand the output, and effectively manage your codebase.

Understanding the differences between Git branches is a fundamental skill for any developer. Whether you're reviewing a feature branch before merging, debugging a regression, or simply trying to grasp the evolution of your codebase, Git provides powerful tools to visualize these changes. This article will guide you through the most common and effective commands to compare branches, helping you interpret the output and make informed decisions.

The Basics: git diff for Branch Comparison

The git diff command is your primary tool for comparing changes. When used with two branch names, it shows you the differences between the tips of those branches. By default, it compares the working directory against the index or the last commit. However, its true power shines when comparing two distinct references, such as branches, commits, or tags.

git diff <branch1> <branch2>

Basic syntax for comparing two branches.

This command will display all changes that exist in <branch2> but not in <branch1>. The output will show additions (prefixed with +), deletions (prefixed with -), and modifications. It's crucial to understand the order: git diff A B shows what's in B that's not in A.

flowchart LR
    A[Branch A] --> C(Common Ancestor)
    B[Branch B] --> C
    C --> D{git diff A B}
    D --> E["Changes unique to Branch B (relative to A)"]

Conceptual flow of git diff A B showing changes unique to B.

Comparing with the Common Ancestor: git diff ...

Often, you don't just want to see the raw differences between the current states of two branches. Instead, you might want to see what changes a feature branch introduces since it diverged from its base branch. This is where the three-dot notation (...) comes in handy. git diff <branch1>...<branch2> compares <branch2> with the common ancestor of <branch1> and <branch2>. This is particularly useful for code reviews, as it shows only the changes introduced by <branch2> relative to where it branched off from <branch1>.

git diff main...feature-branch

Comparing a feature branch against its common ancestor with 'main'.

flowchart LR
    subgraph History
        C1(Commit 1) --> C2(Commit 2)
        C2 --> C3(Commit 3 - Common Ancestor)
    end

    C3 --> M1(main: Commit 4)
    C3 --> F1(feature-branch: Commit 5)
    F1 --> F2(feature-branch: Commit 6)

    style C3 fill:#f9f,stroke:#333,stroke-width:2px

    M1 -- "git diff main...feature-branch" --> D["Changes in feature-branch since C3"]
    F2 -- "git diff main...feature-branch" --> D

Illustrating git diff main...feature-branch comparing feature-branch to the common ancestor.

Viewing Commit-by-Commit Differences: git log

While git diff shows the cumulative changes, git log allows you to see the individual commits that are present on one branch but not another. This is invaluable for understanding the history and progression of changes.

git log <branch1>..<branch2>
# Or, to see commits on feature-branch not on main:
git log main..feature-branch

Viewing commits unique to one branch.

This command shows all commits reachable from <branch2> that are not reachable from <branch1>. In simpler terms, it lists the commits that <branch2> has that <branch1> does not. You can add --oneline --graph for a more condensed and visual output.

git log --oneline --graph main..feature-branch

A more visual way to see unique commits.

Advanced Comparison Options

Git offers several flags to refine your diff output:

1. Word-level diff

Use --word-diff to highlight changes at the word level instead of line level. This is particularly useful for text documents or code where only a few words on a line have changed.

2. Show only file names

Add --name-only to see just the names of the files that differ between the branches. This is great for a quick overview.

3. Show stats

The --stat flag provides a summary of changes, including the number of insertions and deletions for each file.

4. Graphical diff tool

If you have a graphical diff tool configured (like Beyond Compare, Meld, or VS Code's built-in diff), you can use git difftool <branch1> <branch2> to open the comparison in your preferred GUI.

git diff --word-diff main feature-branch
git diff --name-only main feature-branch
git diff --stat main feature-branch
git difftool main feature-branch

Examples of advanced git diff options.