How to compare files from two different branches in Git

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.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources

Attributions:
  1. undefined by undefined