Git

Finding diff Between Current and Last Version

On Git, developers add several changes to the Git repository when required. Additionally, they can view the log history of the Git references and add changes. Developers are allowed to find the difference between the source code’s updated version and the previous version of the file. The “$ git diff” command can be used to perform this operation.

This post will provide a way of finding the difference between the current and latest versions.

How to Find the diff/difference Between Current and Last Versions of the Same File?

To find the difference between the current and previous versions of the file, check out the given below steps:

  • Move to the Git local directory
  • Check the list of repository content
  • Select and update the desired file
  • Check the Git log history and copy desired commit SHA hash
  • Run the “$ git diff <1-SHA-hash> <2-SHA-hash>” command.

Step 1: Navigate to Git Repository
First of all, switch to the required Git repository by utilizing the “cd” command:

$ cd "C:\Users\nazma\Git\Test_13"

Step 2: View List of Content
Then, run the “ls” command to check the list of repository existing content:

$ ls

According to the provided image, the current repository contains four text files:

Step 3: Open Desired Existing File
Next, run the “start” command along with the particular Git file to open it:

$ start file1.txt

As a result, the file will open with the default editor, add, and save changes. Then, close the file:

Step 4: Track Changes
After that, execute the “git add .” command to push all changes from the working area to the tracking index:

$ git add .

Step 5: Commit Changes
Next, save all changes to the Git repository by committing through the provided command along with the desired commit message:

$ git commit -m "file1.txt re-updated"

Step 6: View Git Log History
To view the Git log reference history, run the “git log” command:

$ git log .

As you can see, the list of all added commits has been displayed along with the SHA hash and commit message. Select commit SHA hash to find the difference between them. For instance, we have highlighted the chosen SHA hash:

Step 7: Find Difference Between File Current and Previous Version
Finally, run the “git diff” command along with the selected SHA hash:

$ git diff d351073 43e84c6

Here, the difference is displayed as follows:

  • a/file1.txt” represents the previous version of the particular file.
  • b/file1.txt” indicates the current version of the updated file.
  • ” symbol assigned to the older version.
  • +++” symbol represents the changes in the file’s current version:

That’s all! We have elaborated on finding the difference between the older and the latest version of the same file.

Conclusion

To find the difference between current and older versions, first, move to the directory and then view the content list. Select and open the desired file. Add and save changes. Then, track and commit the modifications to the staging area. Check the Git log history and copy the desired commit SHA hash against the same file. Lastly, run the “$ git diff <1-SHA-hash> <2-SHA-hash>” command. This post demonstrated the method of finding a difference between the current and latest versions.

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.