GIT Compare Two Branches

Git is the most popular version control system. Many developers and teams use Git for their activities. One common practice when working with Git is to create branches that help create a separate working environment. With branches, you can mess around with things without affecting the other sections of the code, and at long last, you can compare your branches and then merge them. The question is, “how do you compare two branches using Git?”

Comparing Branches in Git

Git offers the Git diff command, which lets you easily compare branches. You can compare the files in the branches and their commits. Let’s see the various ways of comparing branches.

1. Comparing Two Branches

Creating a new branch and working on it is safe when you’ve cloned a project. That way, you separate it from the main branch without messing up things. Before you merge the two branches, you should compare them to see the differences.

The syntax for comparing branches is:

$ git diff branch0..branch1

In the syntax above, you are trying to check what files or information are available in branch1 but not in branch0. Ideally, the double-dot will compare the branches’ tips or HEAD. If solving a conflict between the branch, the double-dot will give more details about the branches.

In the image above, we are comparing two branches: linuxhint and master. We can see all the commits in the master branch that are not in the linuxhint branch.

2. Comparing Files in Branches

It’s common to have specific files sharing the same name but in different branches. It could be some code or a file and you want to check the difference between the two versions of the files in different branches. In that case, use the syntax below.

$ git diff branch0..branch1 -- filename

With the above syntax, we are comparing the given file based on the HEAD of the two branches to outline the difference in the versions.

For instance, in the image below, we can see the difference in the file named one.txt in the file’s content. The words in the two files are not the same. This command could come in handy if you compare code that had a conflict or your version of the code with another.

3. Comparing Commits

Different branches have different commits, when working on the same version of a project in a separate branch, it makes sense that you would want to check the difference in the commit of two branches. Here, you will use the git log command with the syntax below.

$ git log branch0..branch1

In the example below, we can see the different commits for each branch, the date of the commit, the author, and the commit message. You can also note the commit ID. Comparing commits between branches is a great way of analyzing a merge conflict.

4. Using Triple-dots

Most comparisons involve the HEAD, which is why you will often use the double-dots. However, if you need to compare one branch with the ancestor of both branches, you use the triple-dots.

In the syntax below, we are comparing branch1 with the common ancestor of branch0 and branch1.

$ git diff branch0...branch1


Git is an excellent and easy-to-use version control system. It offers short commands that achieve great functionality, when working on a project, it’s recommended to create a branch to act as the safe zone for trying things without messing with the original code. This guide covered the various ways of comparing branches on Git to see their difference in commits, HEAD, and files.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.