Difference Between Branches in Git

Abdul Jabbar Feb 17, 2022
  1. Get the Difference Between Branches in Git
  2. Use the git diff Command to Compare Two Branches in Git
  3. Use the Double-Dot Notation to Compare Two Branches in Git
  4. Use the Triple-Dot Notation to Compare Two Branches in Git
Difference Between Branches in Git

In this article, we will get to know about comparing two Git branches using three different techniques.

Get the Difference Between Branches in Git

Git is a well-known software that helps us track changes we made in the files and also helps to build a unique version of a commit for all the changed files. If mistakenly something went wrong, we can restore the perfect version of that file in the branch very easily.

We commonly use different branches to perform our work accurately and up to the mark not connected with the original files. We need to merge all those different branches we made to get the accurate output result in our original repository branch for this kind of circumstance.

For that, we need to sort out the difference between two or more branches.

Merging two branches is significant because before merging the whole content into the main branch, we should know that there is no conflict between branches that we are merging and the code is stable.

It is very important to check in the most stable code so the team can continue working, and it should not be a road blocker for them.

Use the git diff Command to Compare Two Branches in Git

When we need to check the difference between the two files for merging, we have to run the following command to find the differences and merge them into a single file (the most recent change). We will use the git diff command to check the difference between the two branches.

This command will compare the two branches files, showing us whether there is a conflict occurring or not in that file’s content.

Diffing is a well-known feature in Git that differentiates between two data sources given as the input. And these data sources could be anything, for example, branches, files, commits, or more related to the code or text.

For instance, if we compare two different branches, namely feature and master, we will use the following command to get the difference between the files and codes of these branches.

$ git diff master feature

Use the Double-Dot Notation to Compare Two Branches in Git

The following command can also compare the content in the particular branch by applying the two dots between the branches.

$ git diff master..feature

There is no difference between the result of the two commands.

The purpose of both commands is to compare the tips, or in other words, we can say the latest commit on the two branches and show us the difference.

Use the Triple-Dot Notation to Compare Two Branches in Git

The comparison with the three-dot(...) notation will differ from the two commands above.

Note that it will not differentiate the tips of both branches but will only compare the tip of the feature branch with the primogenitor commit of both branches.

This command will list all the changes made on the master branch since the feature branch was developed.

The following command compares two branch content using the three dots notation.

$ git diff master...feature

The two dots notation command is mostly used to compare two Git branches’ contents.

Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.

LinkedIn

Related Article - Git Diff