Git Diff



The git diff is a command-line utility that takes two input data sources and outputs the changes between them. It's a multi-use git command.

The input data source of a git diff command can be files, commits, branches, and more.

One of the main objectives of using a version control system is to track changes in a project. And understanding those changes is what helps to understand how a project grows. This is where the git diff command is helpful to display modifications in detail.


Track modifications that have not been staged

The regular use of git diff command displays the changes added to the working directory compared to the staging area.

For example, we have edited the file1.txt, and we want to see the changes that are not staged yet. So we can see changes using the git diff command as follows:

$ git diff
diff --git a/file1.txt b/file1.txt
index a4ae562..75640d3 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1,3 +1,3 @@
 hello world

-Hola
+More changes here for test

The above output shows the changes made on file1.txt using the git diff command.


Track modifications that have staged but not committed

The git diff command offers an option --staged to track changes that have been staged but not committed yet.

The git diff --staged command displays differences between the HEAD and the staging area.

In the last section, we modified the file1.txt, so to show an example for the --staged flag, we will add the file1.txt to the staging area, and after we will run the git diff --staged command as follows:

$ git add file1.txt 
$ git diff --staged
diff --git a/file1.txt b/file1.txt
index a4ae562..75640d3 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1,3 +1,3 @@
 hello world

-Hola
+More changes here for test

The above command will show the changes between the staging area and the HEAD.

Note: HEAD is a reference to the last commit in the current checkout branch, or we can say it is a reference to the current branch.


Track modifications that have been committed

The git diff HEAD command shows the difference between the HEAD and the working directory, so it's displays modifications that have been committed.

We will continue with our example, so we will commit the modifications of the file1.txt and add some additional changes after the commit and run the git diff HEAD command as follows:


$ git commit -m "file1 modified"
[new_branch c3bf48e] file1 modified
 1 file changed, 1 insertion(+), 1 deletion(-)

$ echo "changes added after commiting the file" >> file1.txt

$ git diff HEAD
diff --git a/file1.txt b/file1.txt
index 75640d3..ac612b9 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1,3 +1,4 @@
 hello world

 More changes here for test
+changes added after commiting the file

The above output shows the difference between the HEAD and the working directory using the git diff HEAD command.


Track modifications between two commits

Git allows displaying changes between two commits, whether it is the latest commit or the old commit, using the git diff <commit1-sha1> <commit-sha2>

To show changes between two commits, we need the SHA of commits that we can have running git log command and after that running git diff command as follows:

$ git diff 4b3251c c3bf48e
diff --git a/file1.txt b/file1.txt
index a4ae562..75640d3 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1,3 +1,3 @@
 hello world

-Hola
+More changes here for test

The above output display all the modifications made on file1.txt from the commit 4b3251c (most recent) to the commit c3bf48e (previous).

Note: The order of commits that we pass to the command git diff is important. The common way is the pass the recent commits for the first parameter and the olds one on the second.


Track modifications between two branches

Git offers the possibility to compare branches. The comparison between branches will be critical, especially when analyzing branch before merging to avoid conflicts.

the git diff command provides the comparison between branches using the following syntax

$ git diff <branch-1> <branch-2>

The bellow command display changes between the master and the new_branch

$ git diff master new_branch
diff --git a/file1.txt b/file1.txt
index 3b18e51..75640d3 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1 +1,3 @@
 hello world
+
+More changes here for test


ExpectoCode is optimized for learning. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2020-2021 by ExpectoCode. All Rights Reserved.