How to compare two commit ranges in git with git range-diff


Git is a free, open-source, version control system that can be used to keep track of changes that goes into any project file. It can also be used by multiple individuals who wish to collaborate on any software development project.

The git range-diff function is used to compare two git ranges. It takes in two commit ranges which are to be compared as arguments in the command line. It then displays the comparison between the two commits, which includes any deleted, modified, or newly added lines.

Real-world example

Now that we have a basic understanding of what git and the git range-diff function are, let's look at an example to understand further how this function is used.

Let's say we are working on a large project and wish to incorporate a new feature into the project, so we create a new branch and start working on the feature. We push any updates onto the branch that we have just created.

After we are finished with the new feature, we merge the branch on which the feature resides with the main branch. Now we want to verify if all the changes we did on the feature branch were present on the main branch.

Diagram showing the branches
Diagram showing the branches

For this, we will use the git range-diff function. We will give it the commit ranges of the feature branch(f1—f3) and the merged branch(f1`—f3`) from the first commit to the last commit. We will then cross-check to verify if all changes were successfully incorporated into the main code.

Implementation

Now that we have understood the purpose of this function, we will now understand how we are going to implement it.

Installing git and required packages

First, we will install git so that we can access and manipulate repositories. We can see the steps to install git below.

  1. First, we will open the terminal. Linux-based systems can use the shortcut "Ctrl + Alt + T" or simply open the terminal through the search bar.

  2. Once inside the terminal, we will enter the following command to install git (for Ubuntu-based systems).

apt-get install -y git
Command to install git
  1. We need to ensure the version of git we are installing is 2.23 or higher. To check the version of our git installation, we can use the command below.

git --version
Command to check git version
  1. Next, we have to install the git-extras package that contains the git range-diff function. We will use the command below to install the package.

apt-get install -y git-extras
Command to install git extras package

Cloning the repository

Now that we have installed git, we will clone the repository on which we will compare commit ranges. We can see the steps to clone a git repository below.

  1. First, we will first get the repository link from GitHub.

  2. Once we have the link, we will then open the terminal. In the terminal, we will enter the following command to clone the repository.

git clone <link to repository>
Command to clone a git repository
  1. Next, we will change directories to the one where our project files are contained using the command:

cd <directory name>
Command to change directories
  1. We can also see the commit made to the project by viewing the logs using the command:

git log
Command to show changes in a git repo

The git log command shows an output similar to the code snippet attached below. Here we can see hash values against the commit, which we can use as input ranges in the git range-diff command.

commit b323b8b9805c47391b591m331b88fd4ba9d20830 (HEAD -> main, origin/main, origin/HEAD)
Author: Educative <Educative.io>
Date: Thu Dec 29 14:48:39 2022 +0500
Added NewFile
commit 1fe39fc275m2761hz2a67dc29820e104b6fb3e6bc
Author: Educative <Educative.io>
Date: Thu Dec 29 14:47:27 2022 +0500
Delete Old_File.docx
commit 1385274hc911b98cde80d3f583779b90c11a96b
Author: Educative <Educative.io>
Date: Thu Dec 29 14:47:07 2022 +0500
Update README.md
git log command output

Note: The above output contains commit hashes that are not useable. The user must use their commit hash values for the command to work.

Using the git range-diff command

Now that we are in the git directory, we will use the git range-diff command here and provide the commit ranges as arguments to the command.

Below is the syntax for the command to compare the commit ranges.

git range-diff <f1>..<f3> <f1`>..<f3`>
Command to find range difference

In the command above, <f1>..<f3> <f1`>..<f3`> represent the git ranges that are to be compared.

We can simply replace f1, f3, f1`, and f3` with commit hashes of the commit ranges we wish to compare.

Live demonstration

Here we can see a terminal that we can use to execute the command we had gone through above.

Below is a list of commands that we covered above. We can use these commands in the terminal to understand how the git range-diff function work.

apt-get install -y git
git --version
apt-get install -y git-extras
git clone <link to repository>
cd <directory name>
git log
git range-diff <f1>..<f3> <f1`>..<f3`>
Summary of all commands

Note: Replace <link to repository> with your own git repository link and the <f1>..<f3> <f1`>..<f3`> values with your repositiories commit hashes.

Terminal 1
Terminal
Loading...

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved