git diff usage explained [Multiple Scenarios]

 

Introduction to git diff

A diff function is a command-line tool that displays the differences in outputs of two data sources in a git environment.  Git diff command helps to show a comparison between files, branches, tags and commits that make up a commit-tree. It can bring out the distinction between various versions of commits history and therefore a powerful tool to use in monitoring the project progress. Diff function is supported by the git log and the git status command when it comes to maintaining the status updates.

Among the changes you can monitor using diff function include differences between a merge, differences between two trees, differences between the index and active tree among others.

Advertisement

In this tutorial, we shall explain different scenarios for working with git diff using examples.

 

Git diff workflow

git-diff-image
git diff workflow

The above image is a simplified illustration of how the git diff command works as summarized below;

  • git-diff-HEAD~1 is the distinction between HEAD~1 and the working tree.
  • git-diff–HEAD is the difference between HEAD and the working tree.
  • git-diff-staged is the distinction between the HEAD and the index.
  • The difference between HEAD~1 and index is git-diff-staged-HEAD~1
  • git–diff is the difference between the index and working tree.

 

git diff syntax

You can use git diff along with following supported options. To get more details on individual supported options, you can refer official git documentation:

git diff [<options>] [<commit>] [--] [<path>…​]
git diff [<options>] --cached [--merge-base] [<commit>] [--] [<path>…​]
git diff [<options>] [--merge-base] <commit> [<commit>…​] <commit> [--] [<path>…​]
git diff [<options>] <commit>…​<commit> [--] [<path>…​]
git diff [<options>] <blob> <blob>
git diff [<options>] --no-index [--] <path> <path>

 

Setting up the lab environment

To practice using the diff command, first, we will have to set up the lab environment. I will therefore clone the remote project git-diff-c to my local workstation that I will be using for this tutorial. I'm using windows 10 pro and git version 2.32.0.windows.2.

See below the expected output:

$ git clone https://github.com/J-Mulombi/git-diff-c.git
Cloning into 'git-diff-c'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.

 

git diff examples

Scenario-1: Use git diff to track changes between the active local directory and the index

We will use git diff command to show the distinction between the staged commits in the index and active directory.

Advertisement

In this example, we shall first create a new branch mybranch from the master branch in our git-diff-c local repository as follows:

$ git checkout -b mybranch
Switched to a new branch 'newbranch'

Let’s now add two files test-1.txt and test-2.txt to the active branch mybranch as shown below:

$ touch test-1.txt

$ touch test-2.txt

$ git add .

Next, we shall run the git status command to view the new files in staging area as illustrated below:

$ git status
On branch newbranch
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   .test-1.txt
        new file:   test-2.txt

After adding two files test-1.txt and test-2.txt to the active branch mybranch, we shall proceed to modify both files.

$ echo "edited file" >> test-1.txt

$ echo "edited file" >> test-2.txt

Next, we shall run the git status command again to view  test-2.txt and test-1.txt files unstaged as displayed below:

$ git status

Sample output:

git diff usage explained [Multiple Scenarios]

Notice from the above output both files test-1.txt and test-2.txt have been modified, unstaged and waiting to be committed.

To illustrate how git diff shows the differences in the local directory and the index, let's again modify  test-2.txt file at this stage. Then we will proceed to run the diff command to see the difference.

Advertisement
$ echo "added first line in test-2 file" >> test-2.txt

Check the difference in the local repo:

$ git diff

Sample Output:

git diff usage explained [Multiple Scenarios]

From the output, the diff command has displayed the distinction between two versions of test-2.txt file located in the staging area.

 

Scenario-2: Apply diff command to track the differences between the active directory and the last commit

You can track changes between the last commit and the active directory as follows:

Let's again make some changes to test-2.txt file before running the diff command in this example.

To see the distinction between the active directory and our last commit, we shall run the git diff HEAD command as illustrated below:

$ echo "added third line in test-2 file" >> test-2.txt


$ git diff HEAD
diff --git a/test-1.txt b/test-1.txt
new file mode 100644
index 0000000..5d3aabf
--- /dev/null
+++ b/test-1.txt
@@ -0,0 +1 @@
+edited file
diff --git a/test-2.txt b/test-2.txt
new file mode 100644
index 0000000..c88866e
--- /dev/null
+++ b/test-2.txt
@@ -0,0 +1,3 @@
+edited file
+added first line in test-2 file
+added third line in test-2 file

Sample Output:

git diff usage explained [Multiple Scenarios]

Note that the output shows the changes between the active repository and the last commit. It shows the history of all commits when we first added files test-1.txt and test-2.txt to the latest modification done to the test-2.txt file.

 

Scenario-3: Use diff to track the difference between the staging area and the last commit

You can track the difference between the last commit and the staging area as follows:

In this example, we shall stage file  test-3.txt that we had earlier created and modified. So how do we track the differences between the last commit and the staging area?

Here I have test-4.txt in the staging area while test-2.txt is waiting for the commit:

git diff usage explained [Multiple Scenarios]

To track the differences between the last commit and staging area, run the git diff --staged HEAD command as demonstrated below:

$ git diff --staged HEAD
diff --git a/test-4.txt b/test-4.txt
new file mode 100644
index 0000000..e69de29

 

According to the output, test-4.txt is a staged commit, and it is yet to be committed. You can compare the output with git diff HEAD:
git diff usage explained [Multiple Scenarios]

So we learned that:

  • git diff --staged will only show changes to files in the "staged" area.
  • git diff HEAD will show all changes to tracked files. If you have all changes staged for commit, then both commands will output the same.

 

Scenario-4: Track the distinction between git commits using the diff operator

You can track the differences between two commits as shown below:

While on mybranch we shall run the git log --oneline command to see all the commits.

$ git log --oneline
2ae9f25 (HEAD -> mybranch) edited file test-2.txt
9ec46d0 (origin/master, origin/HEAD, master) Initial commit

 

Next, we shall run the diff between commit 2ae9f25 and 9ec46d0.

$ git diff 2ae9f25 9ec46d0
diff --git a/test-1.txt b/test-1.txt
deleted file mode 100644
index e69de29..0000000
diff --git a/test-2.txt b/test-2.txt
deleted file mode 100644
index e69de29..0000000

The above results display the difference between commit 9ec46d0 and the 9ec46d0initial commit on the active branch mybranch.

 

Scenario-5: Use diff command to track the difference between git branches

To track the difference between git branches you shall run the git diff <branch A> <branch B> commanded as illustrated below:

$ git diff master mybranch
diff --git a/test-1.txt b/test-1.txt
new file mode 100644
index 0000000..e69de29
diff --git a/test-2.txt b/test-2.txt
new file mode 100644
index 0000000..e69de29

The results display the commits differences between the master branch and mybranch in the remote repository git-diff-c. This diff information is vital, especially when working on a collaborated project to manage conflicts.

 

Summary

We have been able to cover the following topics about diff function:

  • The meaning of git function
  • The git diff operator workflow
  • Git function working scenarios

 

Further Reading

Git-diff

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment