How to compare two branches in Git?


Collaborators will use multiple branches in order to have clearly separated codebase. At some point in time, we may have to merge these branches in order to have the resulting work in the main branch. It is important that we compare the differences in the branches before merging to avoid any conflicts. We will see a couple of different ways to compare two branches −

  • Listing commit differences − This method shows commits that are present in a branch but unavailable in the other branch.

  • Listing file changes − This method compares branches and displays how exactly a certain file is different in the two branches.

From the below diagram we can easily understand that there are two branches in the repository one is the master and the other is a feature branch. Each commit and their corresponding commit message are shown in the diagram. From the diagram, it is clear that initial commit hash is “9260faa”

The following command can be used to get a similar graphical representation of branches.

$ git log −−oneline feature −−all −−graph

In the output given below ‘*’ denotes a commit and we could see that there are 4 asterisks which means there are 4 commits. “9260faa” is the initial commit. There are two branches emerging from the initial commit, the master branch on the left and a feature branch on the right.

delI@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git log −−oneline feature −−all −−graph
* 0f26bbe (HEAD −> master) bonjour add
|* eOccfce (feature) howdy file add
|* 71b4d39 how are you added
|/
* 9260faa hello. txt

Listing Commit Differences

At some point in time the feature branch has to be merged to master, as it is our main line of work. The syntax to list commit differences using the git log command is given below −

$ git log <branch1>..<branch_2>

The following command compares two branches and returns the commits that are present in the feature branch but are unavailable in the master branch.

$ git log master..feature −−oneline

The output is shown below −

e0ccfce (feature) howdy file add
71b4d39 how are you added

Listing File Differences

If we want to compare two branches on the basis of changes that have been performed on the files, we need to use the diff tool. The syntax to use the diff tool is −

$ git diff <branch_name>

The following command lists differences in files in the current branch (master) and the feature branch.

$ git diff feature

This will give a very detailed difference. If we don't want to see the detailed differences, but need to know only what files are different use the −−name−only. This is shown in the below example.

$ git diff −−name−only feature

The output is shown below −

bonjour.txt
hello.txt
howdy.text

The −−name−status flag can be used with the diff tool to show both the file name and its status.

$ git diff −−name−status feature

The output is shown below. The left column shows the file’s status and the right column shoes the file’s name.

  • Denotes that the file is present in the current branch but unavailable in the other branch.

  • Denotes that the file is modified and its content is different in the two branches

  • Denotes that the file is deleted from the current branch but is available in the other branch.

dell@DESKTOP−N961NR5 MINGW64 /e/tut_repo (master)
S git diff −−name−status feature
A bonjour . txt
M hello. txt
D howdy. text

dell@DESKTOP−N961NR5 MINGW64 /e/tut_repo (master)
$ git diff −−name−only feature
bonjour. txt
hello.txt
howdy. text

Updated on: 20-Feb-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements