In this tutorial, we’re going to be talking about how to compare two git branches. I’ll show you how you can get the difference between two git branches.
Git as you know is the software to tracks the changes in the files and makes a separate version of that commit for all changes files. So in case of something happen wrong then you can restore the correct version of the file.
GitHub is the internet hosting web interface software, where you can host your files. It keeps your original files and changes in the file store in a different version if you have committed to it.
In GitHub, you can create repositories and branches as you want. A repository can have many branches like if we are working on a project then branches could be production, dev, or staging, etc. You can create accordingly and give a name appropriately.
It is common to use different branches in order to have work clearly and neatly separated from original files.
So, when you will work on different branches, you might want to merge branches to get the final output result in the main branches. Here you need to check the difference between the two branches or many.
So, always check the difference between two branches before merging them in the main branch. It will be very helpful for you to check if there is any conflict between branches merging.
So let’s get started and learn how you can compare two git branches.
Compare Two Branches using
You have to use the
git diff command to check the difference between the two branches. It will compare the two branches and tip (HEAD) and return the difference between the two branches.
For example, we want to compare two branches that are
dev then use the following example command to get the difference recap to see the modification.
You can also use this command in another way. Like using the two dots between the branches. It is the same and works as above, we just have to add two dots between branches. See the following command.
You need to understand how it works. Actually, both commands will compare the tips or you can say the latest commit on the branches and show you the difference. Look at the following image for a better understanding.
If you are using the Git software then it uses the color code to show you difference between two branches. The lines in green are those lines that are added to the files and in red that are deleted from the files.
Comparing Two Branches using Triple Dot Notation
Using the three dots (…) notation, the comparison will be different from the above command. It will not compare the tips of both branches. It will only compare the tip of the
dev branch with the common ancestor commit of both branches.
It means that it will list all the changes that were committed on the
master branch since when the
dev branch was created.
See the following command to compare two branches using the three dots notation. And look at the image below for a better understanding three dots concept.
In the most of time, you will want to use the first method (using the two dots between branches) to compare two git branches.
Compare Commits Between Two Branches
In the above methods, we compare the two git branches and get the full details of the differences between them. So instead of getting full details, we can also see the difference between the commits. Git will show you the commits that are different as a result.
This is very similar to the above commands but in this case, we have to use the
git log command instead of
So we will use the
git log command to compare the commits between two git branches and it will give you the list of all different commits of both branches.
The above command will give you all the information of different commits.
You can also short the result of the above command. Instead of getting the full information of commits, we can get the information shorter in a one-line commit.
So use the following flag with the
git log command to get the short commit result different while comparing the commits between two git branches.
Compare Specific File Between Two Branches
Sometimes you only want to check the difference between files of branches, so you can check and get the difference of file as in result. You need to just specify the path of the file of the branch with the
git diff command.
To check the difference between a specific file of two branches, use the
git diff command using the two dots or three dots and specify the file path. See the following git command for this case.
To generate the output of a specific file instead of stdout, you have to use the
--output flag with the
git diff command. See the following command with –output flag.
You can also redirect the output to a file. Use the following command for this.
Compare Two Branches using difftool
git difftool is a Git command that allows you to compare and edit files between revisions using common diff tools.
git difftool is a frontend to git diff and accepts the same options and arguments.
See the following example command to compare the master and dev branch using the
git difftool command.
-d flag is used to copy the modified files to a temporary location and perform a directory diff on them. This mode never prompts before launching the diff tool.
So in this tutorial, we learned how to compare two git branches using the
git diff and
git log commands. We also learned to check the difference between a specific file of two branches.
I hope you got all the points about comparing two git branches. If you still have any questions please ask me in the comment section, I’ll respond to you as soon as possible.