Git diff

Git diff Overview

The diff command in Git is generally a function that inputs two data sets and rolls out the changes made between them. It is a multitasking tool that is used to bring out the differences between two data sources. The data sets can be of any type, be it a commit, a branch, file, or anything in the repository. The diff tool helps in providing the GUI for carrying out these operations. It also helps in identifying the frequently used commands like git status or git log. It also helps in analyzing the states of the present Git repository.

Additionally, the diff command in Git is used to reflect very vital changes. It is used for tracking down changes that reflect the actual difference. This difference can be used to carry out debugging or committing operations in a controlled environment. The diff command is also used to check out the additional details relating to working tress and their feasibility with the project.

How does it work?

1

The above image shows the overall workflow of how the diff command is executed when invoked. Being a developer, you need to make use of this by typing the command below:

$ git diff

The above command will roll out the difference between files if you have earlier initialized them. The output after using this command may look like this:

$ git diff
 [master (root-commit) 6f77fc3] add diff test file
 1 file changed, 1 insertion(+)
 create mode 100644 diff_test.txt 

Before typing the above command, you need to first check the status if the file is present or not. The command is:

$ git status

This will show you if the file is present in your working directory. If not, type the following command:

$ git add <file_name>

This will add a file to your working directory. Now if you check the status it will show the file name along with the extension. The next task if to use the diff command. It will display something like this:

$ git diff
 Warning: LF will be replace by CRLG in kundan.txt
 The file will have its original line endings in your working directory.
 diff --git a/kundan.txt b/kundan.txt
 Index : 307294a ..00baacd 100644
 @@ -1, 2 +1 @@
 This is Kundan Pathak
 This is Kundan 

The above output can be explained as follows:

  1. The first line defines the names that have been taken into consideration by the git diff. There is a visible instance that has been marked by a and b along with two different file states that are taken input. This is of no use since it shows the metadata related to the execution of the command. The hash object value is then defined for its internal use.
  • The next line defines the symbol called legend that tells you what has been used to describe the first line and the second line. You can see the -- is used in the first file and the + is used in the second file. Therefore, whenever you use the diff command, it will show you the changes related to the first file with -- sign and the second file will be marked as + sign.
  • The @@ symbols are used to define the summary of the changes. These symbols are called chunks. In the above output, these symbols tell us that the one or two lines were changes from the first and reflected in the second file. It is important to note that the chunks are used along with - and + signs for stating the changes from the first file to the second file.

Options

$ git diff filename

This will output the changes of that current file from its previous committed stage.

$ git diff branch_name

This will output the modifications of the current branch to the mentioned branch from its previous committed stage.

$ git diff --no-prompt

It stops from prompting the diff tool present in the Git GUI.

Summary

There are still many such options available for the diff process in Git. They can be used with not just the files, but can also be used while committing, branching, etc. It is a handy tool that utilizes an internal working algorithm for fetching out the differences present between two files. It is often used with git log and git status command to conveniently cross perform the tasks required for your projects.