Data report"State of code review 2024" is now liveRead the full report

Using git diff between branches

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

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


This guide will focus on how to use git diff to compare different branches, a command that is especially useful for reviewing changes before merging branches or understanding differences between a local branch and the main branch.

To see the differences between two branches, you can use the following command:

Terminal
git diff branch1 branch2

This command will show the diff output of what is different between the end of branch1 and the end of branch2. It lists all the changes made in branch2 relative to branch1.

Terminal
diff --git a/file1.txt b/file1.txt
index 9d1ed08..4b3792e 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1,4 +1,4 @@
-Hello, this is branch1.
+Hello, this is branch2.
This file contains sample text.
Here we demonstrate a simple diff.
-This line is the same in both branches.
+This line has been changed in branch2.
  • diff --git a/file1.txt b/file1.txt: indicates that the differences are being shown for the file named file1.txt.
  • index 9d1ed08..4b3792e 100644: shows the SHA-1 hashes of the file versions before and after the change along with the file's mode (which remains unchanged here, 100644 indicates a regular file).
  • Lines beginning with - and +:
    • Lines that start with a - show content that exists in branch1 but was changed or removed in branch2.
    • Lines that start with a + show what the content has been changed to in branch2, or what has been added.

In this example:

  • The first line of the file changed from "Hello, this is branch1." in branch1 to "Hello, this is branch2." in branch2.
  • Another line "This line is the same in both branches." in branch1 was changed to "This line has been changed in branch2." in branch2.

If you only want to compare a specific file between two branches, you can specify the file name at the end of the command:

Terminal
git diff branch1 branch2 -- path/to/file

To compare a file named example.txt between develop and main branches:

Terminal
git diff develop main -- example.txt

This will only show you the differences in example.txt and will ignore all other files.

If you are only interested in seeing which files have changed, not the actual line-by-line differences, you can use:

Terminal
git diff branch1 branch2 --name-only

This command will list the filenames that are different between two branches, without displaying the actual content differences.

For generating a patch file from the differences between two branches, use:

Terminal
git diff branch1 branch2 > changes.patch

This command will create a file called changes.patch which can be applied to another branch using git apply. For more information on applying a patch, see this guide on Git patches.

Using git log with diff options can help visualize the commit history alongside the differences:

Terminal
git log --graph --oneline --decorate --all --patch

This displays an ASCII graph of the commit history, showing which commits are included in each branch and what changes those commits introduced.

If you find that git diff does not show the expected changes, ensure that your branches are up to date with their remote counterparts:

Terminal
git fetch origin
git diff origin/branch1 origin/branch2

This compares the remote versions of the branches, which is useful if there have been updates to the branch that you do not yet have locally.

For further reading on examining the differences between branches, 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