Additionally, Git enables its users to check the difference between commits with the help of the “$ git diff” command, and in this study, we will discuss it in detail.
What is the Difference Between Two Commits in Git?
Sometimes users encounter situations when they are required to compare data in their Git repository with some other data source that is available in another Git repository. In such a scenario, knowing the differentiation between two commits in Git is essential.
To do so, check out the below-provided procedure!
Step 1: Launch Git Bash
Open the Git terminal named “Git Bash” with the help of the “Startup” menu:
Step 2: Create Directory
First, create the new directory using the “mkdir” command:
Step 3: Navigate to Directory
Next, execute the “cd” command to move to the directory:
Step 4: Create File
Now, create the new file in the Git local directory:
Step 5: Add File
Add the untracked file to the Git repository:
Step 6: Commit Changes
Now, commit all changes to the Git directory:
Here, the “-m” option refers to the added message:
Step 7: Create File
Create another new file in the current directory:
Step 8: Add File
Add “file2.txt” to the Git repository using “git add” command:
Step 9: Commit Changes
Execute the “git commit” command with the “-m” flag to save changes in the Git directory:
Note: To view the difference between two commits, open created files in an editor and make some changes like adding some text, save file and commit changes.
Step 10: Check Log History
Check the log history of the Git directory utilizing the “git log” command:
As you can see, all commit changes of the Git repo are displayed. Here, the “–pretty=oneline” option is used to show the output as one commit per line:
Step 11: Check Difference Between Commits
Now, execute the “git diff” command to differentiate the two commits:
As you can see, we have committed both files “file1.txt” and “file2.txt” two times. The “—” symbol indicates the first commit, and the “+++” symbol represents the second commit in both files. Additionally, “@@ -0,0 +1 @@” shows the line number we have changed in each file:
That’s it! We have briefly differentiated the two commits in Git.
To understand the difference between two commits in Git, first, open the terminal, navigate to Git local repo using the “cd” command and create a new directory. Then, create and add new files. Commit changes to the Git directory by executing the “$ git commit -m” command and run the “$ git log –pretty=oneline” command to check log history. Next, execute the “$ git diff” command to check the difference. This study demonstrated how to differentiate two commits in Git.