Git Diff

Learn via video courses
Topics Covered

Overview

The git diff command is one of the most important git commands which can be used to see the change differences between various states of the file such as the difference between the working directory and staging area, staging area and git repository, etc. Additionally, the git diff command can also be used to compare the change differences between the branches.

Pre-Requisites

It is necessary to have the basics of git clear such as working, staging, and repository area. Also, you should know the following four git commands before moving ahead in this article:

  1. git init
  2. git add
  3. git commit
  4. git status

What is the Git Diff Command, and Why Do We Need it?

Have you ever faced a situation when you wanted to see the difference between the changes you made in a file in your working directory and the file state present in your staging area or the git repository? If you are a frequent user of git, then you must have faced such a situation. The git diff command can be very helpful in such situations.

Now you know why you need the git diff command and how it can be utilized. But still, let me tell you, it's a kind of theoretical definition; here we go.

Definition: git diff is a git command that is used to see the change differences between various states of the file, such as the difference between the working directory and staging area, the difference between the staging area and the repository, and the difference between the working directory and the repository.

In other words, we can also say that with the help of the git diff command, we can see what things have been added or deleted in the file. Simple as that.

How to Use the Git Diff Command?

If you know how to use the basic git commands such as git init, git add, git commit, etc., then usage of the git diff command is also the same. Now let us move on to the most interesting part of this blog, which is understanding the output of the git diff command.

Lets us modify a file by adding and deleting some data in it so that we can see the result after running the git diff command. Here we go,

GIT DIFF

I have a folder called 'demo' in which I have created a new file called demo.txt, initialized the git repo, added some text in the file, and staged the file using the git add command. Now let us delete some data and run the git diff command to see the output.

GIT ADD COMMAND

In the above-given image, you can see the output, which looks a bit complicated. Let us understand each line in the output one by one.

Breaking Down the Git Diff Output From the Above-Given Image.

  1. The first line diff --git a/demo.txt b/demo.txt shows that the comparison is between the two different versions of the same file. Those different versions of file 'demo.txt' are version a and version b, where a is the older version and b is the newer version.

  2. The second line index b0370eb..d5a69ff 100644** consists of some metadata having two hashes( **b0370eb** and **d5a69ff** ) of both versions and an internal file mode modifier **100644`.

  3. The third line --- a/demo.txt has a --- symbol, also called 'legend' which shows that the - (minus) sign is assigned to the a version.

  4. The fourth line +++ b/demo.txt shows that + (plus) sign is assigned to the b version.

  5. The fifth line @@ -1 +1 @@ is called chunk, which has chunk headers (two @ symbols both at the start and at the end). The -1 and +1 tells that 1 line is removed from starting of line 1 and

  6. The 6th line -I am learning new git command shows the state of the a version(previous state).

  7. The seventh line \ No newline at the end of file says that there's no newline after the modified line, that is, after 'I am learning new git command.'

  8. The eighth line +I am learning shows the state of the b version (current state).

  9. The ninth line \ No newline at the end of file is explained in point 7 above.

Scenarios Where We Can Utilize the Git Diff Command

Some scenarios where we can utilize the git diff command are:

  • To track the changes between working directory and Staging area
  • To track the changes between Staging area and git repository
  • To track the changes between working directory and git repository

So let us create a file called `name. txt' and initialize a git repository in this file. After that, open the terminal side-by-side to look at changes without the hassle of switching the tabs again and again. Here we go,

GIT DIFF COMMAND

In the above-given image, you can see that I have added some text, 'I am Sufiyan', in the file called name.txt. Now let us see the current status of the git repository using the git status command.

GIT STATUS

So it can be seen in the output that the file's name appeared in red, which states that the file is not getting tracked, so let us track the file using the git add command and run the git status command again.

GIT STATUS COMMAND

So name.txt is appearing in green, which means that it's getting tracked. After that, we also ran git diff, but nothing appeared in the output. Why? It's because git diff checked for the change difference between the working directory and the staging area, and since there's no difference at all, it shows nothing in the output.

Now let us add some new text 'Khan' in the file and run the git diff command.

GIT DIFF COMMAND

In the above-given image, you can see that after adding a new file, we got the output from the git diff command as expected. This is due to the change difference between the working directory and the staging area.

Now let us add the changes and commit them. After doing that, git diff shows nothing, as there's no difference left in the working directory and staging area. Let us now add a new line and add it to the staging area. Now to see the difference between the staging area and the git repository, we need to run the git diff --staged command or git diff --cached command, as both commands are the same but have different names.

GIT COMMANDS

Now you can see that the output compares the change differences between the staging area and the git repository.

Conclusion

  • git diff is a git command that is used to see the change differences between various states of the file, such as the difference between the working directory and staging area, the difference between the staging area and the repository, and the difference between the working directory and the repository.

  • The usage of the git diff command is also the same as other git commands such as git init, git add, git commit, etc.

  • We can also say that with the help of the git diff command, we can see what things have been added or deleted in the file. Simple as that.

  • The first line diff --git a/demo.txt b/demo.txt shows that the comparison is between the two different versions of the same file. Those different versions of file 'demo.txt' are version a and version b, where a is the older version and b is the newer version.

  • The second line index b0370eb..d5a69ff 100644 consists of some metadata having two hashes(b0370eb and d5a69ff) of both versions and an internal file mode modifier 100644.

  1. The third line --- a/demo.txt has a --- symbol, also called 'legend' which shows that the - (minus) sign is assigned to the a version.
  • The fourth line +++ b/demo.txt shows that + (plus) sign is assigned to the b version.

  • The fifth line @@ -1 +1 @@ is called chunk, which has chunk headers (two @ symbols both at the start and at the end). The -1 and +1 tells that 1 line is removed from starting of line 1 and

  • The 6th line -I am learning new git command shows the state of the a version(previous state).

  • The seventh line \ No newline at the end of file says that there's no newline after the modified line that is after 'I am learning new git command.'

  • The eighth line +I am learning shows the state of the b version (current state).

Also, see

  1. What is GitHub?
  2. Git Vs GitHub.