Git users use Git for managing DevOps projects and their source code. On this platform, the project developers work together on project features and functions through files and are required to modify these files from time to time. As many developers work on the same project, they may occasionally need to view the previous and new changes in the file for understanding.
This article will demonstrate how to differentiate the same file between two commits on the same branch.
How to diff Same File Between Two Commits on the Same Branch?
To diff a file between two commits on the same branch, first open the Git repository. Then, commit the changes. After that, in the same branch, modify the file that was recently committed and commit it again to save local changes. Now, utilize the “git diff HEAD~1 HEAD” command.
Check out the provided steps for practical demonstration.
Step 1: Open Git Terminal
First, from the Start menu, open the “Git Bash” Git terminal:
Step 2: Go to Git Repository
Next, utilize the “cd” command and open the Git repository:
Step 3: Initialize Git Repository
Initialize the Git repository through the “git init” command:
Step 4: Generate New File
To generate a new file and save the file content directly, execute the provided command:
In the above command, the echo will add the content and directly save it in the “NewFile.txt” file:
Now, use the “ls” command to view all files and verify whether the file is created or not:
Step 5: Add File to Staging Index
Next, move the untracked file to the staging index by utilizing the Git “add” command:
Check the Git status to verify whether the changes are added to the tracking index or not:
Step 6: Commit Generated File
To commit the newly generated file to save changes, write the following command:
Step 7: Modify File
Open the file in the text editor and update it:
Modify the file content and hit the “Ctrl+S” key to save the file:
Step 8: Move File to Staging Index
After that, add the untracked modification in the staging index:
Check the repository state to verify if the changes are added in the staging area or not:
Step 9: Commit Modified File
After that, add the modifications in Git local repository using the given command:
View the repository log to verify whether the changes are committed or not:
Step 10: Diff Same File Between Two Commits
Next, diff the same file between two commits by executing the below-mentioned command. Also, provide the commit ids for those two commits:
The below output successfully shows the difference between the same file between two commits. Here, the signs “—” and “+++” are output indicators that show the new and old commits. :
Alternatively, Git users can utilize the HEAD position instead of commit ids to view the difference as shown in the below command:
We have taught you how to differentiate the same file between two commits.
Conclusion
To diff the same file between two commits, first go to the Git local repository. Do the first commit for the file using the “$ git commit -m” command. Then, make some modifications in the same file and commit it again to save changes in the local repository. After that, show the difference of the same file between different commits using the “git diff HEAD~1 HEAD” command. This post demonstrated how to diff a file between two commits.