What is the git diff <branch1>..<branch2> command?

The git diff <branch1>..<branch2> command compares the HEAD (tip) of two specified branches and reports the differences. It displays all commits present in the second branch that are missing from the first branch.

The process is illustrated below:

Options

The options of the git diff <branch1>..<branch2> command are as follows:

  • <branch1>: The first of the two branches to be compared.
  • <branch2>: The second of the two branches to be compared.

Usage

To demonstrate the git diff <branch1>..<branch2> command, let’s first create two branches in a git repository.

The main branch will contain a solitary text file labeled main.txt. To upload the file to the main branch, you can use the following commands:

git add main.txt
git push origin main

Note: For details on how to push files to a Github repository, please visit the following link.

Next, let’s add a file alternative.txt to a new branch alternative within the same repository through the following commands:

git checkout -b alternative
git add alternative.txt
git commit -m "add alternative"
git push --set-upstream origin alternative

The git checkout -b alternative command creates a new branch called alternative. The --set-upstream flag is required in the push command because no reference to this branch exists yet.

At this point, the only difference between the main and alternative branches is that the alternative branch also contains the file alternative.txt.

To display the differences between the branches, the git diff command can be used as shown below:

git diff main..alternative

The above command produces the following output:

widget

The file alternative.txt is present in the alternative branch but not in the main branch. The green lines in the output represent the exact lines of the files that differ between the two branches.

Note: For more details and variants of the git diff command, you can check the official documentation.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved