How to compare a local branch to a remote branch in Git

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


Comparing a local branch to its remote counterpart in Git is an important operation for developers to understand the differences or changes before pushing or pulling data. This guide explains how to use Git to compare local branches to remote branches, focusing on various aspects such as overall branch differences, specific file comparisons, and more.

Before diving into commands, it's important to understand that when you compare a local branch to a remote branch, you are checking for differences in commits, the tree, and ultimately the files themselves. This helps ensure that you're fully aware of what changes will be pushed or what changes need to be pulled.

Before comparing, make sure your local repository's information about remote branches is up to date:

Terminal
git fetch

This command downloads objects and refs from another repository. It does not change any of your local branches, making it safe for updates before a comparison.

To compare your local branch to a remote branch, you can use the git diff command which shows the differences between two branches:

Terminal
git diff localbranch..origin/remotebranch

Replace localbranch with your local branch name and remotebranch with the remote branch name. This command will output differences between the tips of both branches.

If you need a list of changed files along with summaries of changes, use:

Terminal
git diff --name-status localbranch..origin/remotebranch

This modification of the diff command provides a concise list of files with status indicators (added, modified, deleted).

For a more granular look at what each commit introduced:

Terminal
git log localbranch..origin/remotebranch --oneline

This shows commit logs that are in the remote branch but not in the local branch, helping identify new changes.

If you're interested in comparing a specific file between your local and remote branch:

Terminal
git diff localbranch..origin/remotebranch -- path/to/file

This command focuses the diff output on a specific file, making it easier to pinpoint changes.

For more complex differences or when a visual side-by-side comparison is preferred, configure Git to use an external diff tool like Meld, Beyond Compare, or others:

  1. Configure the diff tool:

    Terminal
    git config --global diff.tool meld
  2. Use the tool for comparison:

    Terminal
    git difftool localbranch..origin/remotebranch

This opens the differences in the external tool, providing a more user-friendly way to compare changes.

  • Regularly pull from remote: Always pull changes from the remote branch regularly if others are also working on the same project to minimize conflicts.
  • Use tags for important comparisons: If comparing between releases or milestones, use tags to make comparisons more meaningful and easier to manage.
  • Review before pushing: Always review the differences before pushing to ensure that no unintended changes are included.

For further reading see the official Git documentation.

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2