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.
Basic usage of git diff
for branch comparison
Comparing two branches
To see the differences between two branches, you can use the following command:
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
.
diff --git a/file1.txt b/file1.txtindex 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 namedfile1.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 inbranch1
but was changed or removed inbranch2
. - Lines that start with a
+
show what the content has been changed to inbranch2
, or what has been added.
- Lines that start with a
In this example:
- The first line of the file changed from "Hello, this is branch1." in
branch1
to "Hello, this is branch2." inbranch2
. - Another line "This line is the same in both branches." in
branch1
was changed to "This line has been changed in branch2." inbranch2
.
Advanced usage
Comparing specific files between branches
If you only want to compare a specific file between two branches, you can specify the file name at the end of the command:
git diff branch1 branch2 -- path/to/file
Example:
To compare a file named example.txt
between develop
and main
branches:
git diff develop main -- example.txt
This will only show you the differences in example.txt
and will ignore all other files.
Additional git diff
options and features
List only filenames
If you are only interested in seeing which files have changed, not the actual line-by-line differences, you can use:
git diff branch1 branch2 --name-only
This command will list the filenames that are different between two branches, without displaying the actual content differences.
Output differences as a patch
For generating a patch file from the differences between two branches, use:
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.
Viewing diffs with commit graphs
Using git log
with diff options can help visualize the commit history alongside the differences:
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.
Common issues and solutions
Branch not up-to-date
If you find that git diff
does not show the expected changes, ensure that your branches are up to date with their remote counterparts:
git fetch origingit 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.