Git

What is the Difference Between Two Commits in Git

In this era, Git is the most widely used and demanding decentralized versioning system. Its working depends on two fundamentals, making changes by the users and then successfully committing those changes to the Git repository. These commits save the most recent commit and keep track of the changes made to the project.

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:

$ mkdir my_dir

Step 3: Navigate to Directory

Next, execute the “cd” command to move to the directory:

$ cd my_dir

Step 4: Create File

Now, create the new file in the Git local directory:

$ touch file1.txt

Step 5: Add File

Add the untracked file to the Git repository:

$ git add file1.txt

Step 6: Commit Changes

Now, commit all changes to the Git directory:

$ git commit -m "add file1"

Here, the “-m” option refers to the added message:

Step 7: Create File

Create another new file in the current directory:

$ touch file2.txt

Step 8: Add File

Add “file2.txt” to the Git repository using “git add” command:

$ git add file2.txt

Step 9: Commit Changes

Execute the “git commit” command with the “-m” flag to save changes in the Git directory:

$ git commit -m "add file2"

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:

$ git log --pretty=oneline

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:

$ git diff

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.

Conclusion

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.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.