How to use git diff command?

git diff command is used to show the differences between the content of your files and the last commit. You can also compare the files with the previous commits only by passing commit SHAs to git diff. If you are interested in learning the diff between commits, visit this page: How to show diff between commits in Git?

It is essential that you understand the output of git diff command. In the output, you will find two symbols: + and -.

  • Lines that you have added to a file are shown with a + sign in front of them.
  • Lines that you have removed from a file are shown with a - sign in front of them.
  • Lines that you have updated are shown with a - sign in front of the old version and a + sign in front of the new version.

There are four situations that you will face when you use git diff command:

  1. If a file is in a modified state.

In this situation, when you run git diff command, Git compares your modified file with the contents of your last commit.

The content of index.html file after the last commit:

<p>First paragraph.</p>
<p>Second paragraph.</p>

The content of index.html file after modification:

$cat index.html
<p>First paragraph.</p>
<p>Third paragraph.</p>

$git status
On branch testing
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

        modified:   index.html

no changes added to commit (use "git add" and/or "git commit -a")

Here, index.html file is modified.

$git diff
diff --git a/index.html b/index.html
index c18576a..a9425b5 100644
--- a/index.html
+++ b/index.html
@@ -1,2 +1,2 @@
 <p>First paragraph.</p>
-<p>Second paragraph.</p>
+<p>Third paragraph.</p>

From the output of git diff, you can infer that <p>Second paragraph.</p> is deleted and <p>Third paragraph.</p> is added.

  1. If a file is in a staging area.

When you run git diff command, you will get nothing in the output because git diff compares the files against the staging area files. If you have already staged the changes, then there is nothing to show.

But there is a command-line option that will allow you to compare staged changes against the last commit. That command-line option is --staged.

The content of index.html file after the last commit:

<p>First paragraph.</p>
<p>Second paragraph.</p>

The content of index.html file which is present in a staging area:

$cat index.html
<p>First paragraph.</p>
<p>Third paragraph.</p>

$git status
On branch testing
Changes to be committed:
  (use "git reset HEAD ..." to unstage)

        modified:   index.html

In the above output, you can see that index.html file is in a staging area.

$git diff
$git diff --staged
diff --git a/index.html b/index.html
index c18576a..a9425b5 100644
--- a/index.html
+++ b/index.html
@@ -1,2 +1,2 @@
 <p>First paragraph.</p>
-<p>Second paragraph.</p>
+<p>Third paragraph.</p>
  1. If the same file is in a modified state and staging area.

The content of index.html file after the last commit:

<p>First paragraph.</p>
<p>Second paragraph.</p>

The content of index.html file which is present in a staging area:

<p>First paragraph.</p>
<p>Third paragraph.</p>

The content of index.html file after modification:

$cat index.html
<p>First paragraph.</p>
<p>Third paragraph.</p>
<p>Fourth paragraph.</p>

$git status
On branch testing
Changes to be committed:
  (use "git reset HEAD ..." to unstage)

        modified:   index.html

Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

        modified:   index.html

You can see that index.html file is in a modified state as well as a staging area.

When you run git diff, Git will compare the modified file against the same file present in the staging area.

$git diff
diff --git a/index.html b/index.html
index a9425b5..7802f93 100644
--- a/index.html
+++ b/index.html
@@ -1,2 +1,3 @@
 <p>First paragraph.</p>
 <p>Third paragraph.</p>
+<p>Fourth paragraph.</p>

You can see that in the modified version of index.html file, a paragraph <p>Fourth paragraph.</p> is present. <p>Fourth paragraph.</p> is not present in the staging version of index.html file.

On the other hand, if you run git diff --staged, Git will compare staged changes against the last commit.

$git diff --staged
diff --git a/index.html b/index.html
index c18576a..a9425b5 100644
--- a/index.html
+++ b/index.html
@@ -1,2 +1,2 @@
 <p>First paragraph.</p>
-<p>Second paragraph.</p>
+<p>Third paragraph.</p>
  1. If a file is neither in a staging area nor in a modified state.

When you run git diff or git diff --staged command, you will get nothing in the output.

$git status
On branch testing
nothing to commit, working directory clean
$git diff
$git diff --staged