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.
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.
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.
Now that we have understood the purpose of this function, we will now understand how we are going to implement it.
First, we will install git so that we can access and manipulate repositories. We can see the steps to install git below.
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.
Once inside the terminal, we will enter the following command to install git (for Ubuntu-based systems).
apt-get install -y git
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
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
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.
First, we will first get the repository link from GitHub.
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>
Next, we will change directories to the one where our project files are contained using the command:
cd <directory name>
We can also see the commit made to the project by viewing the logs using the command:
git log
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 +0500Added NewFilecommit 1fe39fc275m2761hz2a67dc29820e104b6fb3e6bcAuthor: Educative <Educative.io>Date: Thu Dec 29 14:47:27 2022 +0500Delete Old_File.docxcommit 1385274hc911b98cde80d3f583779b90c11a96bAuthor: Educative <Educative.io>Date: Thu Dec 29 14:47:07 2022 +0500Update README.md
Note: The above output contains commit hashes that are not useable. The user must use their commit hash values for the command to work.
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`>
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.
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 gitgit --versionapt-get install -y git-extrasgit clone <link to repository>cd <directory name>git loggit range-diff <f1>..<f3> <f1`>..<f3`>
Note: Replace <link to repository> with your own git repository link and the <f1>..<f3> <f1`>..<f3`> values with your repositiories commit hashes.
Free Resources