How to Show Diff Details for Uncommitted Changes in Git

John Wachira Feb 15, 2024
How to Show Diff Details for Uncommitted Changes in Git

This article outlines how we can get the diff details for uncommitted work in Git. We use the git diff command to show the difference between various Git references, for example, commits, index, and working trees.

We can employ the command to display the details of our uncommitted work, as we will discuss below.

Show Diff Details for Uncommitted Changes in Git

For easier context, we will employ an example. Let’s assume the image below represents the current state of our working directory in our repository.

working directory

Both files above fall under the uncommitted work category. However, the git diff command does not work the same for staged and unstaged files.

For a clearer picture, let’s start with the basic git diff without parameters.

$ git diff

Output:

git diff no param

You will notice that the git diff command without a parameter will only show the changes for your unstaged files. From the Git documentation, the git diff command without parameters will display changes relative to the index.

These changes should go to the index but are not yet added.

How do we display the diff details for the staged files?

To show the changes for staged files, we use the git diff command, as illustrated below.

$ git diff --cached

Outcome:

git diff –cached

We can see that adding the --cached flag to our git diff command will show the diff details for our staged file. True to that, the Git documentation states that the git diff --cached will show staged changes relative to the current commit (HEAD).

What if we do not want to run the commands separately?

To display the diff details for both staged and unstaged changes, run the git diff command, as illustrated below.

$ git diff HEAD

Outcome:

git diff HEAD

The git diff HEAD command will display the diff details between the working directory and the current commit.

Bonus Tip

If you are having difficulty reading the diff details above, you can use a third-party difftool like Meld.

In a nutshell, we can manipulate the git diff command to display the diff details for our uncommitted work, depending on the category of your files. We have seen how we can show the diff details for staged and unstaged files in Git.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Diff