In the last tutorial about the useful Commands in Git Log, we went across some of the very popular and important commands in git log. These are used very frequently in Git. In the list of those commands, I also mentioned briefly about git diff command. This tutorial is all about git diff command.

Pre-Requisites

What is Git Diff Command?

Diff command is used in git to track the difference between the changes made on a file. Since Git is a version control system, tracking changes are something very vital to it. Diff command takes two inputs and reflects the differences between them. It is not necessary that these inputs are files only. It can be branches, working trees, commits, and more. We will be making use of git diff command as we will progress deeper into the course as defining everything in one place will not be feasible. Checking the difference made in two files being the most common operation used in git diff, we will proceed with the same intentions.

For using diff command first we have to make some changes to the files. We will be making the changes to the already existing file harish.txt and writing "This is Harish" in it. You can use Vi Editor for the same. After that, enter the following command:

git diff

Git Diff

Note: Diff Command will produce the changes in all the files that are present. For the changes on some specific files only, type the name of the file after the command name.

Let's see now how git diff command responds to the operations we just did.

Now let us breakdown the response given by git diff and understand everything one by one.

  1. The first line shows the file names that have been considered as the input in git diff. You can see that they have been marked by a and b along with the two different file state that has been taken as input.
  2. This line is not of use. This shows the metadata related to the command and the execution of it on the files. As you must be aware by our discussion in Dot Git folder, this is the object hash value required by Git for internal use.
  3. This line defines the symbol, called a legend, to tell you what is used to describe the first file and what is used to describe the second file. As you can see, - is used in front of the first file, and + is used in front of the second file. So whenever diff shows you the changes related to the first file, they will be marked by - and the changes in the second file will be marked by the symbol +.
  4. The fourth line shows you symbol @@ and symbols ahead of it. They are called chunks. Chunks in git diff define the change' summary. In our image below the following chunk can be seen @@ -1,2 +1 @@
  5. This means that lines one and two were changed in the first file and line one was changed in the second file. Remember the - and + symbol used in the third point as a symbol to the first and second file respectively.

Git Diff Commands

Now let us consider three cases for diff commands and see how does it respond.

  • Git Diff when Data is added in a file
  • Git Diff when Data is deleted in a file
  • Git Diff with Color Words Option

How does Git Diff behave when data is added to a file?

In this section, we will see how the git diff command behaves when the data is added to the file. But before that, we must clear out the changes that we saw in the above sections by committing them. To commit, first, we need to add the files to the staging area.

Type the following command to add the changes to the staging area: git add .

Git Add All

Once everything is done. Commit the changes using the command: git commit

This will open up the notepad to enter the commit message. Enter the appropriate commit message and close the editor. After we are done with committing the changes, check if git diff is showing anything or not by typing the following command: git diff

Git Diff Check

As we can see, there are no changes, and hence we can continue adding the content to our file harish.txt.

Previously the content was This is Harish. I have added Rajora at the end of the sentence making the complete sentence as This is Harish Rajora. Now after adding the word, let us execute the diff command and see what happens.

Git Difference After Adding Content

It is quite understandable that line one has changed in both versions of the file with changes been reflected.

How does Git Diff behave when data is deleted in a file?

After we are done with the above section, commit the changes so that there is no change left to show. Confirm it by using the same diff command as we did above. After that, delete the last name Rajora which we added above to the original sentence This is Harish.

Execute git diff command to see the changes

Git Difference After Deleting Content

There is nothing to explain in this screenshot. Try to revise the points mentioned in the earlier section in case of doubts.

How to see diff changes in one line?

It is quite evident that we are seeing the latest changes in the above screenshot in the green color. But the red color sentence is not needed that much. Since sometimes, we are interested only in the changed text and not how it looked previously. This does not mean we do not want to see the changes that have happened or the difference. We will clear it out by using an option color word in diff command.

To use the option, type the command: git diff --color-words

Git Difference Color Words Option

Now the changes can be seen in one line only. The word in red is depicting that it has been deleted from the original file.

Git Diff is not limited to this only, it is used exhaustively for many other purposes which you can learn here. You must have also understood that this is an essential command which will be used very commonly. We will be exploring other parts of this command when we move towards branching and other such concepts. Till then keep practicing, and we will move on to our next tutorial now.

Harish Rajora
I am a computer science engineer. I love to keep growing as the technological world grows. I feel there is no powerful tool than a computer to change the world in any way. Apart from my field of study, I like reading books a lot and developing new stuff.
Reviewers
Lakshay Sharma's Photo
Lakshay Sharma

Similar Articles

Feedback