How to find the difference between two commits in git
The git working phenomena is based on two fundamentals: changes made by the contributors and then committing those changes successfully to the git repository. The successful commit ensures to keep the last commit as well keeping the project in version control category. Additionally, git offers to check the difference between two commits; for that, it has a command named “git diff” and it triggers the “diffing function”. This function takes two parameters as an input and marks the differences between those parameters; the parameters are basically the commits of a file.
Generally, if you run the git diff command; you will experience an output that will display the uncommitted changes referring to the last commit:
Let’s start to dig into the depth of applying the diff command of git:
To find the difference between two commits; first, we will make some changes and commit them to the git repository.
Step 1: Make changes to git repository
We have created a directory in our git repository and added a text file into it; the following commands will help to create and move the terminal to diff directory:
$ cd diff
Once you have navigated to the “diff” directory; use the command to create as well as edit the text file:
Step 2: Initialize the git repository and commit the changes
After making changes to the repository, now you have to commit the changes; before committing you must initialize the git repository by issuing the below mentioned command:
Now add the text file by following the command written below:
Once the above commands are executed successfully; you are ready to go for committing; for this, use the command mentioned below to commit changes:
Step 3: Make more changes to file
Considering the above changes, we have only performed a single commit to the file; so, a comparison for change cannot be drawn; in short, if we try to apply diff command at this stage, it will not display any difference. In order to make it functional, we must make some more changes to the text file:
You can use the following command to edit the text file and add some more text or replace the text with new one: we have added a new line on the same text file:
Step 4: Apply the diff git command
Upon successful completion of first three steps, you can now use the diff git command to get the difference between both commits:
The command contains two parameters that refers to two commits in a file “new.txt”:
The output contains following information: index followed by hash refers to the object version of git; the “—” and “+++” symbols are used to identify the changes from first and second commit respectively. Moreover, “@ -1 +1,[email protected]” represents the line number of changes in each file; for instance, “-1” shows the first commit was on the first line and “+1,2” shows that the next commit was on the second line of the file and instance, “–” is prefixed to a numeric number and that number represents the line where the first commit took place; similarly, “+” is prefixed to represent the line number where the second commit was applied.
In industrial projects, it is observed that changes are inevitable, and they must be carried out to accomplish specific goals. These changes are not defined to one stakeholder, that means multiple contributors have to work on the project to carry out those changes and it would be difficult to monitor the modifications in this regard. Git offers extended functionality to track the changes in projects as Git is preferred because of its version control functionality. So, using git would be beneficial to keep an eye on the versions created after the adjustments and one can check the difference between recent commits. In this guide, we have demonstrated a step-by-step procedure to find the difference between two commits. For this, the basic usage of git diff command is explained and verified by applying it on a git repository.