| by Arround The Web | No comments

git diff Between Two Different Files?

On Git, developers deal with several source code files of team projects in multiple ways, such as creating or updating files, comparing them with each other, or finding the difference between them. Moreover, they can perform these tasks by utilizing Git’s different commands. For instance, to get the difference between two files, the “$ git diff” command can be used. This particular command helps developers to view the changes in the Git repository.

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:

$ cd "C:\Git"

Step 3: Initialize Git Repository

To initialize the Git repository, run the “git init” command:

$ git init

Step 4: Create a New File

Next, create a new file and update it with some content through the below-listed command:

$ echo "File 3" > File3.txt

Step 5: Generate Another Text File

Similarly, create another file and perform the same operation:

$ echo "File 4" > File4.txt

Step 6: Verify Created Files

Then, execute the “ls” command to ensure if the files are created:

$ ls

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:

$ git add File3.txt File4.txt

Step 8: Check Git Status

To check the current status of the working repository, use the following command:

$ git status

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:

$ git commit -m "File3 and File4 are committed"

Step 10: Check Git Log

Then, view the Git repository reference log history and verify committed changes:

$ git log

Step 11: Find Comparison Between Two Different Files

To find the comparison between two different files, simply run the given command:

$ git diff HEAD:File3.txt File4.txt

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.

Share Button

Source: linuxhint.com

Leave a Reply