Git How to See the Difference between Two Branches

n Git, branches help you isolate changes from the main branch to a feature branch. Once the work is done, you usually want to combine your feature branch with the main branch. Before doing this, you need to know what’s the difference between the main branch and your development branch.

Here’s a short overview of how you can check the differences between branches in Git.

To find the difference between two branches, use the git diff command and specify the branches separated by two dots:

git diff branch1..branch2

This compares the differences between the tips of branch1 and branch2.

To find the difference between two branches from where they were split to the tip of the new branch, use git diff with three dots instead of two:

git diff branch1...branch2

To find the commit difference between the two branches, use the git log as follows:

git log branch1..branch2

To find the difference in a file between two branches, run git diff with the — <file> option.

git diff branch1..branch2 .. <file-name-or-path>

This is a comprehensive guide to comparing Git branches to find the differences between the two branches. It will teach you how the previously mentioned comparison mechanisms work with useful examples and illustrations. After reading this guide, you know how to see the differences between branches as well as specific files. You also know how to check the commit differences only.

Let’s jump into it!

How to Find Differences between Git Branches

The two main ways to find the difference between two Git branches is by running the git diff with either two or three dots between the compared branches. Most of the time, the former is what you want to do.

Let’s take a closer look at what these commands do and what’s their difference as well.

git diff branch1..branch2 (2 Dots)

To easily find the difference between two Git branches, use the git diff command by specifying the two compared branches separated by 2 dots.

git diff branch1..branch2

For example, if you have the main branch and a feature branch, you can check the difference between the two with:

$ git diff main..feature

This command checks the difference between the tips of the two branches (HEAD). Besides, it will show you what’s different between the two branches.

Another way to say it is, you can use this command to check what commits branch1 has but branch2 doesn’t.

Here’s an illustration of checking the difference between the main and feature branches using the above command:

git diff main..feature

Example

Let’s see a concrete example of how this works with an example project.

Here’s an example log of my example project’s main branch:

 $ git log --oneline
42442b0 (HEAD -> main, origin/new-code-changes, origin/main, origin/feature, origin/HEAD) Update test.txt
4a734bf Create test.txt file
b8e3511 Initial commit

There are three commits made to this branch.

And here’s the log of the feature branch (forked from the main branch):

 $ git log --oneline   
7219142 (HEAD -> feature) Add a new Python file
42442b0 (origin/new-code-changes, origin/main, origin/feature, origin/HEAD, main) Update test.txt
4a734bf Create test.txt file
b8e3511 Initial commit

As you can see, the feature branch has one more commit than the main branch. More specifically, the extra commit adds a new file to the project.

But let’s verify this by checking it with the git diff command you just learned.

 $ git diff main..feature
diff --git a/example.py b/example.py
new file mode 100644
index 0000000..e69de29

The output “new file mode 100644” suggests the difference between the main and feature branch is the addition of a new file, which is indeed the case. Also, you can see the name of this new file one line above in a/example.py b/example.py (twice because Git compares two versions of the same file).

In the output a/example.py b/example.py the a/ and b/ are called prefixes that show the source (a/) and origin (/b) for the comparison. If you want to silence the prefixes, call git diff with the –no-prefix option: git diff main..feature –no-prefix

git diff branch1…branch2 (3 Dots)

Another way to see the difference between branches is by using a very similar command to the previous section. The only difference is instead of using 2 dots, you’re going to use 3.

git diff branch1...branch2

This command checks the difference between the tip of branch2 and the last common ancestor (the commit after which the branches were split).

This is best demonstrated by an illustration.

git diff main…feature

Notice that most of the time, you want to use the command with only two dots.

This is because when developing a new feature in a feature branch, there might be new commits to the main branch made by other developers. To see what’s different between the feature branch and the latest version of the main branch, you need to use the git diff with two dots as it compares the heads of the branches.

Example

Let’s see a concrete example.

Here’s the log of my example project’s main branch:

 $ git log --oneline
25377fe (HEAD -> main) Change the contents of the test.txt file
42442b0 (origin/new-code-changes, origin/main, origin/feature, origin/HEAD) Update test.txt
4a734bf Create test.txt file
b8e3511 Initial commit

And here’s the log of my feature branch:

 $ git log --oneline
7219142 (HEAD -> feature) Add a new Python file
42442b0 (origin/new-code-changes, origin/main, origin/feature, origin/HEAD) Update test.txt
4a734bf Create test.txt file
b8e3511 Initial commit

As you can see from the logs, the latest (top-most) commit in the main and feature branches differ from one another.

The commit with the hash “42442b0” is the latest common commit (common ancestor) between the two branches. It was the last commit before I created the feature branch.

Now, let’s see what git diff says with three dots between the branches:

 $ git diff main...feature
diff --git a/example.py b/example.py
new file mode 100644
index 0000000..e69de29

This suggests that the only difference between main and feature is the new file I added in the feature branch. But remember that there’s also a new commit in the main branch which is not shown in this output.

This is because git diff with 3 dots compares the tip of the feature branch to the common ancestor commit (which is the commit after which I created the feature branch). When compared to the common ancestor, there was no new commit in the main branch yet because the main and the feature were the same branch.

For comparison, let’s run the git diff command that uses two dots between the compared branches:

 $ git diff main..feature 
diff --git a/example.py b/example.py
new file mode 100644
index 0000000..e69de29
diff --git a/test.txt b/test.txt
index c23e6c4..654c7f4 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,3 @@
-this is a change to the test.txt file
+This is just a test commit! I'm adding a new text file to the project on the remote repo.
+
+Let's add another line of text to the file in the remote repo and try fetching the changes locally.

As you can see, this command not only shows the file committed to the feature branch but also the file change made in the main branch that’s not present in the feature branch.

This highlights the difference between using 2 dots and 3 dots in diffing two branches. It also shows why you almost always want to use the 2-dot git diff command instead of 3. The former shows what’s the real difference between the branches before a merger.

Compare Commits between Two Branches

There are times when you might only be interested in the commit difference between two branches. This can be useful if you want to take a high-level view of what’s changed between 2 branches instead of looking at the details of the changes.

To do this, use the git log command and specify the branches you want to compare as follows:

git log branch1..branch2

This command only shows the difference in commits, not the details about the changes made in the commits.

Example

Let’s have a look at a concrete example similar to previous examples.

Here’s the git log for my example project’s main branch:

 $ git log --oneline
25377fe (HEAD -> main) Change the contents of the test.txt file
42442b0 (origin/new-code-changes, origin/main, origin/feature, origin/HEAD) Update test.txt
4a734bf Create test.txt file
b8e3511 Initial commit

And here’s the log for the feature branch.

 $ git log --oneline
7219142 (HEAD -> feature) Add a new Python file
42442b0 (origin/new-code-changes, origin/main, origin/feature, origin/HEAD) Update test.txt
4a734bf Create test.txt file
b8e3511 Initial commit

By taking a look at the commit history between the two branches, you can see that the last commits are different from one another.

Now, let’s verify this by running the git log branch1..branch2 command on these two branches.

 $ git log feature..main
commit 25377fe1b8d7d6378e829dcf914d5a7da65c0838 (main)
Author: artturijalli
Date:   Thu Nov 24 11:10:03 2022 +0200

    Change the contents of the test.txt file

This shows the commit that’s present in the main branch but not in the feature one.

Now, let’s swap the order of the git log command to see what’s present in the feature but not in the main.

 $ git log main..feature
commit 72191429d3b94b5f991a3fbd0050c9b24973d629 (HEAD -> feature)
Author: artturijalli
Date:   Thu Nov 24 10:47:35 2022 +0200

    Add a new Python file

Compare a File between Two Branches

In Git, you can also check the changes in a particular file between two branches. To do this, use the git diff command by specifying the branches and the path to the file you want to check.

git diff branch1..branch2 -- <file-name-or-path>

This shows the differences in the file contents between the two branches.

Example

Once again, let’s take a look at a concrete example of a project under version control.

Inside the example project, there is a file called test.txt. In Git, there are two branches, main and feature.

In the main branch, I’ve made changes to a file called test.txt. More specifically, I’ve added a text that says “this is a change to the test.txt file“.

These changes aren’t present in the feature branch.

Let’s run git diff between main and feature to see these changes:

 $ git diff feature..main test.txt
diff --git a/test.txt b/test.txt
index 654c7f4..c23e6c4 100644
--- a/test.txt
+++ b/test.txt
@@ -1,3 +1 @@
-This is just a test commit! I'm adding a new text file to the project on the remote repo.
-
-Let's add another line of text to the file in the remote repo and try fetching the changes locally.
+this is a change to the test.txt file

Here the lines of text that start with “” are present in both the main and feature branches’ test.txt files. But the last line that starts with “+” means this line is added to the main branch but is not present in the feature branch.

Summary

Today you learned how to see the difference between two Git branches.

To take home, most of the time the “difference between two branches A and B” refers to the difference between the tip of the branches (HEAD). To check this difference, you can use the git diff command and specify the branches separated by two dots (..).

git diff branch1..branch2

This shows all the information about the differences between the two branches, such as file modifications, additions, and deletions.

To only view the commit difference between the two branches, use the git log command in the same fashion:

git log branch1..branch2

To pinpoint changes in a particular file between two branches, use git diff by specifying the branches and the file name (or path):

git diff branch1..branch2 <file-name>

Sometimes, you might want to check what’s the difference between two branches compared to the point at which the branch was created, that is, the common ancestor. You can do this by running the git diff command with three dots between the branches.

git diff branch1...branch2

Thanks for reading. Happy coding!

Read Also

git pull vs git fetch

Scroll to Top