Signup/Sign In
PUBLISHED ON: JULY 12, 2021

How to Compare two Git Branches?

Branches are used to create an independent work environment where developers can work on new features and experiment with changes without worrying about affecting the rest of the project. It is very common for developers to compare branches and analyze the changes between them before merging or rebasing them. Let's learn how to compare branches in Git.

Comparing Branches

There are a few different things that we can compare between different branches. We can compare the files involved in the two branches or the individual commits of the two branches. We will be using the Git Diff command and the Git Log command to view these changes. Let's see how to use them.

Comparing Branches Using Git Diff

When we say we are comparing branches, what we mean is that we are comparing the tips of the two branches. These are the latest commits of the branches and have the most recent snapshot of the work that was done on the branches. We use the Git Diff command with the double-dot(..) notation to compare these two commits. The following image illustrates the two commits compared by the double-dot notation.

Viewing the commits compared by double dot notation

The syntax to use the above command is shown below.

$ git diff <branch-1>..<branch-2>

There is also a Triple-Dot Notation that can be used to compare branches. We use this to compare the latest commit of one branch with its base on the other branch, i.e., the common ancestor of the two branches. The following image illustrates the two commits compared by the triple-dot notation.

Viewing the commits compared by triple dot notation

$ git diff <branch-1>...<branch-2>

In most cases, we will use the double-dot notation as it compares the most recent versions between the two branches. However, triple-dot notation is preferred when we want to see the changes that we have made to a branch since its inception.

The output of the Git Diff with the double-dot notation is shown below. The output will include the differences between all the files present in the two branches.

Output of the Git Diff command with double dots

Comparing Files Between Two Branches Using Git Diff

Two branches may have different versions of the same file. We can compare these changes by mentioning the file name or the file path to the Git Diff command. We can use either the double-dot or the triple-dot notation according to our requirements. Double dot will compare the file between the tips of the branch, and triple-dot will compare the current version of the file with the version included in the common ancestor.

$ git diff <branch-1>..<branch-2> filename

The following image shows the output given by the above command.

Comparing a particular file between the two branches.

Comparing Commits Between Two Branches Using Git Log

We can use the Git Log command if we want to see the commits that are different between the two branches. Instead of showing detailed changes like Git Diff, it will simply output the commits that are present on one branch but not the other one.

$ git log <branch-1>..<branch-2> 

The above command will give an output like the one shown below.

Viewing the difference in commits between the two branches.

Summary

Branches are an important part of Git workflow as it gives developers the freedom to try out new things without worrying about corrupting the rest of the project. It is a good practice to compare branches before merging, rebasing, or deleting them. Git Diff and Git Log commands are used for this purpose. We use Git Diff when we have to compare the changes in the files between two branches. Git Log is used to view the commits that are present on one branch but absent on the other.



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight