How can I diff two branches in GitHub?

Learn how can i diff two branches in github? with practical examples, diagrams, and best practices. Covers git, github, git-branch development techniques with visual explanations.

How to Diff Two Branches in GitHub: A Comprehensive Guide

Hero image for How can I diff two branches in GitHub?

Learn various methods to compare changes between two Git branches directly within the GitHub interface, from simple URL tricks to advanced pull request features.

Comparing changes between different branches is a fundamental task in Git and GitHub workflows. Whether you're reviewing a feature branch against main, checking for divergences, or preparing for a merge, understanding how to effectively 'diff' branches in GitHub is crucial. This article will guide you through several methods, ranging from quick URL shortcuts to using GitHub's powerful Pull Request interface.

Understanding Branch Diffs

A branch diff highlights the differences in file content, additions, deletions, and modifications between two distinct points in your repository's history. In GitHub, this visual comparison is invaluable for code reviews, understanding development progress, and identifying potential conflicts before they arise. The primary goal is to see what changes exist on one branch that are not present on another, or vice-versa.

flowchart TD
    A[Start] --> B{Choose Diff Method}
    B --> C{URL Comparison}
    B --> D{Pull Request}
    B --> E{Compare Page}
    C --> F[Enter Base & Compare Branches in URL]
    D --> G[Create New Pull Request]
    D --> H[Review 'Files changed' Tab]
    E --> I[Navigate to 'Compare' Page]
    E --> J[Select Base & Compare Branches]
    F --> K[View Diff]
    H --> K
    J --> K
    K --> L[End]

Workflow for comparing branches in GitHub

Method 1: Using GitHub's Compare Page (Manual Selection)

GitHub provides a dedicated 'Compare' page that allows you to select any two branches, tags, or commits within your repository and view their differences. This is a flexible method when you want to compare arbitrary points in your history.

1. Navigate to your repository

Open your repository on GitHub.

2. Access the 'Compare' page

Click on the 'Code' tab, then look for the 'branches' dropdown. Next to it, you'll often see a 'Compare' button or a link that says 'X commits ahead, Y commits behind'. Clicking this will take you to the compare page. Alternatively, you can manually navigate to https://github.com/YOUR_USERNAME/YOUR_REPOSITORY/compare.

3. Select branches for comparison

On the compare page, you'll see two dropdown menus. The left dropdown is for the 'base' branch (the branch you're comparing against), and the right dropdown is for the 'compare' branch (the branch whose changes you want to see relative to the base). Select your desired branches.

4. Review the diff

GitHub will automatically display the diff, showing all commits and file changes between the selected branches.

Method 2: Direct URL Comparison

For a quick and direct comparison, you can construct a URL to immediately view the diff between two branches. This is particularly useful for sharing specific comparisons or for scripting purposes.

The general format for the URL is: https://github.com/YOUR_USERNAME/YOUR_REPOSITORY/compare/BASE_BRANCH...COMPARE_BRANCH

Replace YOUR_USERNAME, YOUR_REPOSITORY, BASE_BRANCH, and COMPARE_BRANCH with your specific details. For example, to compare a feature branch named my-feature against main in a repository my-app owned by octocat:

https://github.com/octocat/my-app/compare/main...my-feature

Example URL for direct branch comparison

The most common and feature-rich way to diff branches in GitHub, especially for collaborative development, is through a Pull Request (PR). Even if you don't intend to merge immediately, creating a PR provides a comprehensive interface for reviewing changes.

1. Create a new Pull Request

From your repository's main page, click the 'Pull requests' tab, then click 'New pull request'. Alternatively, if you recently pushed a new branch, GitHub often provides a banner with a 'Compare & pull request' button.

2. Select base and compare branches

On the 'Open a pull request' page, ensure the 'base' branch (the one you want to merge into) and the 'compare' branch (your feature branch) are correctly selected.

3. Review the 'Files changed' tab

Once the branches are selected, GitHub will automatically display the diff. Navigate to the 'Files changed' tab within the PR interface. Here, you'll see a detailed, file-by-file comparison, including line-by-line additions (green) and deletions (red).

4. Utilize PR features

Within the PR, you can add comments to specific lines of code, request changes, and see a summary of commits. This is the ideal environment for thorough code reviews.