Git is a popular distributed version control system used by many website & software development teams around the world. While working with git, it is common to use multiple branches for development, and there will be occasions when you need to compare branches to understand their difference. In this article, we will look at how to compare difference between two branches in git.
Git Compare Difference Between Two Branches
There are different use cases while comparing git branches. We will cover them one by one. For comparing branches or files in git, we have to use git diff command.
Compare Git branches Using two dots
Let us say you have two branches b1 and b2 then here is the command to compare these two branches. You need to add two dots between the branch names in the command below.
$ git diff b1..b2
In this case, it will show information about all the commits in branch b2 that are not present in branch b1.
You will see the output similar to Linux diff command. Here is an example.
$ git diff master..feature diff --git a/file-feature b/file-feature new file mode 100644 index 0000000..add9a1c --- /dev/null +++ b/file-feature @@ -0,0 +1 @@ +this is a feature file
In the above output, it says that a file has been added in feature branch, that is not present in master branch.
Compare specific file between two branches
The above command will compare the two branches and list all the differences between them. Sometimes this can be a lot of information. In some cases, you just want to compare a specific file between two branches. In such cases, you can add the filename after –, at the end of the above command. Here is the command to compare file data.txt between two branches.
$ git diff master..feature -- data.txt
Compare Branch Using 3 dots
You can also compare two branches using 3 dots. In this case, the git command will compare the right branch in the command, with the common ancestor of the two branches. Here is its syntax.
$ git diff b1...b2
In the above example, git will compare branch b2 with the common ancestor of b1 and b2.
In this case also, you can compare specific files between a branch and its ancestor, by simply adding the file name after –, at the end of the command. Here is an example to compare file data.txt between a branch and its common ancestor with other branch.
$ git diff master...feature -- data.txt
Mostly, people use the two dot method to compare branches.
Compare Commits between branches
Sometimes you may want to view the commit differences between two branches. In such cases, you need to use git log command.
$ git log branch1..branch2
It will not show the file differences between the two branches but only the difference in commits.
$ git log master..feature commit 502a2ated7f88d77e0ab9a4e780b856651c5813b (HEAD -> feature, origin/feature) Author: SCHKN <firstname.lastname@example.org> Date: Wed Dec 4 03:10:01 2021 -0500 feature commit
It is useful to quickly understand who made which commits and when, especially if there is a merge conflict, or the merge result is not as you expected it to be.
Here’s a shorter version of the above command.
$ git log --oneline --graph --decorate --abbrev-commit branch1..branch2
Here is an example.
$ git log --oneline --graph --decorate --abbrev-commit master..feature * 802a2ab (HEAD -> feature, origin/feature) feature commit
In this article, we have seen different commands available to compare two git branches, specific files in those git branches, and even their commits.