Git Tutorial - Diff to Compare Differences

Jinku Hu Feb 15, 2024
  1. Check Differences Between Working Copy and the Repository
  2. Git Diff External Tools
  3. Check Differences Between Staging Area and the Repository
Git Tutorial - Diff to Compare Differences

We will show you how to view the changes or differences between your work copy, which is the files that you’re working on in the main files, and the repository. In the second part, we will demonstrate how to compare between the staging area and the repository.

Check Differences Between Working Copy and the Repository

Once we saved a file, this file on a working copy is different from the one in our repository. But how exactly do we view these changes? Because sometimes we made some changes to a file and we want to view the one in the repository and this happens a lot whenever you’re working with code because you want to know exactly the changes you are doing and how this is going to affect the main project.

So the first thing that we can do is to check the git status and you find one file is modified, meaning it’s different from the one in the main project or the main repository. If you type git diff and hit enter, it is going to show you the differences.

$ git diff
diff --git a/test1.txt b/test1.txt
index e1dd8e3..448ad04 100644
--- a/test1.txt
+++ b/test1.txt
@@ -1,2 +1,2 @@
-This is my first Git repository.
-New added text.
\ No newline at end of file
+This was my first Git repository.
+This line is updated.
\ No newline at end of file

The red text in the git bash shows what is in the repository and the green one indicates how the text is modified.

If you have more files updated after last commit, Git will list the file differences one by one.

Git Diff External Tools

If you haven’t got used to git default diff tool, you could configure the difftool to the one you prefer.

We will show you how to configure kdiff3 as the diff, merge tool of Git.

Change .gitconfig File Directory

In Windows OS, open the .gitconfig file in the directory C:\Users\username and append the text to the file,

[diff]
    tool = kdiff3
[difftool "kdiff3"]
    path = "C:/Program Files/KDiff3/kdiff3.exe"
    trustExitCode = false
[difftool]
    prompt = false
[merge]
    tool = kdiff3
[mergetool "kdiff3"]
    path = "C:/Program Files/KDiff3/kdiff3.exe"
    trustExitCode = false
[mergetool]
    keepBackup = false

Here, path is the installed path of kdiff3, and it could be different on your side.

Configure difftool via Git Bash

You could configure the difftool or mergetool by using the command in git bash, too.

git config --global --add merge.tool kdiff3
git config --global --add mergetool.kdiff3.path "C:/Program Files/KDiff3/kdiff3.exe"
git config --global --add mergetool.kdiff3.trustExitCode false

git config --global --add diff.guitool kdiff3
git config --global --add difftool.kdiff3.path "C:/Program Files/KDiff3/kdiff3.exe"
git config --global --add difftool.kdiff3.trustExitCode false
git config --global --add difftool.prompt false

It has done basically the same thing as you modify the .gitconfig file content.

Check Differences Between Staging Area and the Repository

You could use git difftool command to review the file difference between that in your working copy and that in the repository.

Git Difftool-kdiff3

git diff shows the difference between the working copy and the repository, and after the file is already added to the staging area, git diff will not show any difference of this file because it does not compare the staging area against the repository, so that’s why it looks like everything is up-to-date, even though this file is still different than the one in your repository.

The right command to compare the staging are and the repository is to add option --staged behind git diff.

$ git diff --staged
diff --git a/test1.txt b/test1.txt
index e1dd8e3..448ad04 100644
--- a/test1.txt
+++ b/test1.txt
@@ -1,2 +1,2 @@
-This is my first Git repository.
-New added text.
\ No newline at end of file
+This was my first Git repository.
+This line is updated.
\ No newline at end of file
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook