This write-up will explain the method of finding the difference between two files in Git using the “git diff” command.
How to “git diff” Between Two Different Files?
To find the difference between Git’s two files, first, launch the Git terminal and navigate to the desired Git local repository. Then, create two files, and track them to the staging index. After that, update the repository with the added changes and save it. Lastly, run the “$ git diff HEAD: <file1-name> <file2-name>” command to find the comparison between the two files.
Let’s check out the following steps for a practical demonstration!
Step 1: Open Git Bash Terminal
From the Start menu, search for the “Git Bash” terminal and launch it:
Step 2: Move to Git Repository
Now, execute the “cd” command along with the desired repository path and navigate to it:
Step 3: Initialize Git Repository
To initialize the Git repository, run the “git init” command:
Step 4: Create a New File
Next, create a new file and update it with some content through the below-listed command:
Step 5: Generate Another Text File
Similarly, create another file and perform the same operation:
Step 6: Verify Created Files
Then, execute the “ls” command to ensure if the files are created:
As you can see, newly created files exist in the current Git repository:
Step 7: Add Files to the Git Staging Area
Now, run the given command to add all the untracked files to the staging index:
Step 8: Check Git Status
To check the current status of the working repository, use the following command:
It can be seen that the newly created files are tracked successfully and ready to commit:
Step 9: Commit Changes
Now, commit all added changes to the Git repository by running the “git commit” command along with the desired commit message with the “-m” flag:
Step 10: Check Git Log
Then, view the Git repository reference log history and verify committed changes:
Step 11: Find Comparison Between Two Different Files
To find the comparison between two different files, simply run the given command:
In the below-provided output, the “–” sign indicates the content of “File3.txt” and the “+” sign represents the data of “File4.txt”:
We have compiled the easiest method of getting the difference between two different files.
Conclusion
To view the comparison between two different files, first, go to the Git local repository. Then, create and update files simultaneously by utilizing the “echo” command. After that, move all added changes to the working repository. Finally, use the “git diff HEAD: <file-name> <file-name>” command. This write-up explained the procedure to find git diff between two different files.