Read Anthropic’s case study about Graphite Reviewer

Comparing local and remote branches with `git diff`

Greg Foster
Greg Foster
Graphite software engineer


Note

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


This guide will cover the concepts and detailed commands necessary to compare local and remote branches using git diff.

Before diving into the Git commands, it's important to clarify the difference between local and remote branches in Git:

  • Local branches are branches that exist in your local repository, on your local machine. These branches can be created, modified, and deleted without affecting the remote repository until changes are explicitly pushed.
  • Remote branches represent references to the state of branches in a remote repository at the last time you fetched from them. They are prefixed with the name of the remote, typically origin, followed by the branch name, like origin/master.
Join 20,000+ developers at top companies
Stop wrestling with Git commands
The Graphite CLI takes all the pain out of Git, allowing you to ship faster and stop googling Git commands.
main
diff1
diff2

Before you look at the diff between a local and remote branch you should make sure you run git fetch first. This ensures you have all of the most recent changes from the remote branch.

To see the differences between your local branch and its upstream branch (the remote branch it tracks), you can use:

Terminal
git diff <local-branch> origin/<remote-branch>

This command will show you the differences between your local branch and the remote branch. If you want to directly compare the branch you are currently on to its remote counterpart.

As an example let's say we have two versions of file1.txt, one locally, and one stored in a remote repository. We'll assume that local-branch is ahead of origin/remote-branch with some changes:

Terminal
$ git diff feature origin/feature
diff --git a/file1.txt b/file1.txt
index 7b18d64..4ac9e17 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1,3 +1,3 @@
-Hello, World!
+Hello, Earth!
This is an example file.
It has a few lines of text.
  1. diff --git a/file1.txt b/file1.txt:

    • This line shows the file that has differences between the two branches. The format is a/<filename> b/<filename>, indicating the comparison from a (left side or local-branch) to b (right side or origin/remote-branch).
  2. index 7b18d64..4ac9e17 100644:

    • This line shows the blob identifiers (SHA-1 hashes) for the file before and after the changes. 100644 is the file mode, indicating a normal non-executable file in both branches.
  3. --- a/file1.txt and +++ b/file1.txt:

    • These lines indicate the start of the changes in the file from local-branch to origin/remote-branch. --- a/file1.txt represents the original file in local-branch, and +++ b/file1.txt represents the file in origin/remote-branch.
  4. @@ -1,3 +1,3 @@:

    • This line, known as a "hunk header", shows the lines of context around the changes. The numbers show the starting line and the number of lines displayed from each version of the file. -1,3 shows that the original chunk starts at line 1 and spans 3 lines. +1,3 shows the same for the changed file.
  5. Lines beginning with - and +:

    • Lines that start with - indicate lines that exist in the local branch but were changed or removed in the comparison branch (here, origin/remote-branch). Lines that start with + indicate lines that are added or changed in the origin/remote-branch as compared to local-branch.
    • In the example:
      • -Hello, World! shows that "Hello, World!" was the original line in local-branch but changed.
      • +Hello, Earth! shows that in origin/remote-branch, it has been changed to "Hello, Earth!".

To compare two remote branches, first, ensure your local references are up to date using git fetch, then run:

Terminal
git diff origin/<branch-one> origin/<branch-two>

This will show you the difference between two remote branches using the same diff notation as above.

Join 20,000+ developers at top companies
The best engineers use Graphite to simplify Git
Engineers at Vercel, Snowflake & The Browser Company are shipping faster and staying unblocked with Graphite.
main
diff1
diff2

If you want to see the changes introduced by the remote branch since the last common ancestor, you can use:

Terminal
git fetch origin
git diff ...origin/<remote-branch>

The three-dot syntax tells Git to use the merge base of the branches as the starting point for the diff.

Sometimes, you may need to compare your current branch with a specific commit from a remote branch:

Terminal
git fetch origin
git diff <commit-hash>

This will show differences from your current branch to the specified commit from the fetched remote data.

If you need to compare a specific file between your local branch and a remote branch, use:

Terminal
git diff <local-branch>:<file-path> origin/<remote-branch>:<file-path>

This allows for a file-by-file comparison between local and remote branches.

For further reading on the git diff command, see the official Git documentation.

Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

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