What is the Git Diff Command (with Examples)

Git diff is a Git command that can be used to compare differences between files.

The git diff command runs a diff function on the provided data source (project, directory or filename).

Git Diff: See What has Changed to the File

Use the git diff command to see what has changed.


Subscribe to my Newsletter


Git diff will compare the existing file to the last saved version.

A diff is a formatted display showing the differences between groups of files.

You can use git diff without anything after. This will show you all changes in the repository.

$ git diff

You can also use git diff along with the directory name. This will show you all changes made in the directory.

$ git diff changed-file.txt

Or use git diff along with the filename. This will show you all changes made to the file.

$ git diff changed-file.txt

Reading Git Diff

Whenever you use git diff in staging, you get a text output showing you the changes.

diff --git a/README.md b/README.md
index a76e505..8d0211c 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,4 @@
-# Repository of code used in my YouTube Python Tutorials
+# Repo of Code used in my YouTube Python Tutorials

Let’s break it out

Showing the Comparison Input on the First Row

The first row of the diff output shows the git command (diff --git) and the version a (initial version) and b (updated version) of the file (in this case README.md).

Showing the metadata on the Second row of diff

The second row shows the Git metadata, specifically the index key (hash id) where the change is stored in git object database.

Added and Removed

Lines with a minus sign are where lines are removed and with a plus sign are where lines are added.

The triple pluses and minuses show the files in which changes made and the @@ where the changes were made. In this case, starting at line 1, there was 4 lines, and there are still 4 lines.

--- a/README.md
+++ b/README.md
@@ -1,4 +1,4 @@

Below shows the additions and deletions, line-by-line.

-# Repository of code used in my YouTube Python Tutorials
+# Repo of Code used in my YouTube Python Tutorials

Checking What Will Be Committed

You can view only what is going to be committed by using the git diff -r HEAD command. This will show the latest revision (-r) to the most recent commit (HEAD).

Other Git Methods and Github Posts

Learn Git and Github (Complete Guide)
Basics of Version Control
How to Use Git and Github with VSCode
Get Started With Github
How to Push Code to Github
Enjoyed This Post?