How to compare files from two different branches in Git

Share

Overview

The git diff command is used to perform the diff functionThe diff function computes the difference between pairs of consecutive elements of a numeric vector on Git data sources. For example, commits, branches, files, and so on. It can also be used to compare two files of different branches.

Example 1

The command below can be used to compare files of different branches.

git diff my_master master -- js/test.js
Compare files from two different branches

Explanation

The code above will show the difference between the test.js files in the my_master and the master branch. The -- command denotes the end of command-line flags. This is optional unless Git gets confused if the argument refers to a commit or a file.

Example 2

If we want to compare the test.js file from the current branch to the other branch then the current branch name can be skipped by adding ...

git diff ..my_master -- js/test.js
Compare file of current branch with another branch

Explanation

The code above will show the difference between the test.js files in the current branch and the master branch.

Attributions:
  1. undefined by undefined