Git: How to view working tree changes with ‘git diff’

Updated: January 27, 2024 By: Guest Contributor Post a comment

Introduction

Understanding the changes you’ve made in your working tree is a vital skill when you’re using Git, the distributed version control system. It’s essential for managing and tracking the history of your project efficiently. The git diff command is one of the most frequently used Git commands and for good reason. It allows you to compare files, commits, and branches in your repository. This tutorial aims to introduce the basics of git diff, and gradually move to more advanced usage scenarios.

Getting Started with Git Diff

When you’re working in a Git repository, it’s common to make changes to your code and then need an overview of what’s been altered before you proceed to stage and commit those changes. This is where git diff comes in. To get started, simply run the command with no arguments:

git diff

This compares your working directory (the changes on your computer that you haven’t staged yet) with the index (the changes you’ve staged). The output will look something like this:

diff --git a/file.txt b/file.txt
index e69de29..4b835fd 100644
--- a/file.txt
+++ b/file.txt
@@ -1 +1 @@
-Hello World
+Hello Git

This output tells you that file.txt has changed. The line with -Hello World indicates the line has been removed and +Hello Git shows the added line.

Viewing Staged Changes

Sometimes, you’ll want to review what is about to be committed. To see the changes you’ve staged that will go into your next commit, you can use git diff with the --staged (or --cached) option:

git diff --staged

Additionally, to see a summary of which files have changed with the --name-only flag:

git diff --staged --name-only

This can be quite useful to quickly check which files are going to be committed.

Comparing Specific Files

If you’re only interested in seeing the changes for a particular file, you can pass the file name to the command:

git diff file.txt

This will limit the output of the diff to changes made in file.txt only.

Difference Between Commits

git diff is not limited to unsaved changes and staged changes; it can also be used to compare two specific commits. Here’s the basic usage:

git diff COMMIT1_HASH COMMIT2_HASH

You would replace COMMIT1_HASH and COMMIT2_HASH with the commit hashes you want to compare. The output will display the difference between the snapshots of the repository at those two points.

Comparing Branches

Comparing branches is as simple as comparing commits. If you want to compare your current branch with another branch, say develop, you can do the following:

git diff develop

Or, to compare two other branches:

git diff feature-branch develop

This will show you the differences between the tips of the two branches.

Advanced Options

Tailoring the output of git diff with different options allows you to get exactly the information you’re looking for. Here are some advanced options:

  • --color-words: Instead of showing whole lines that have changes, this flag highlights only the changed words.
  • --stat: View the stats (file change, additions, and deletions) of the commits you are comparing.
  • --no-prefix: Omits the ‘a/’ and ‘b/’ filename prefixes.
  • --patience: Makes git diff use a different algorithm, which can be more efficient for some types of changes.
  • Pathspec: By appending a path after the commit hashes, you can restrict the diff to show changes only within that folder or file path.

Here’s how to use these advanced options:

git diff --color-words

git diff --stat COMMIT1_HASH COMMIT2_HASH

git diff --no-prefix COMMIT1_HASH..COMMIT2_HASH

git diff --patience COMMIT1_HASH COMMIT2_HASH

git diff COMMIT1_HASH COMMIT2_HASH -- 'src/*.js'

Ignoring Space Changes

Whitespace changes can often clutter diff views. Git offers several options to ignore these when running diffs:

  • --ignore-space-at-eol: Ignores changes at the end of line.
  • --ignore-all-space: Ignores all white space changes.
  • --ignore-space-change: Ignores changes in the amount of white space.
  • --ignore-blank-lines: Ignores changes that involve only blank lines.

This can be coupled with other git diff commands:

git diff --ignore-all-space COMMIT1_HASH COMMIT2_HASH

Generating Patches

git diff can also be used to generate patch files, which are text files that describe the changes between two states of a Git repository. Patches can be used to share changes in a human-readable format, or applied using the git apply command. To create a patch:

git diff > changes.patch

Aliasing

If you find yourself using a git diff command with a lot of options frequently, you might want to create a Git alias to save time. You can do this by editing your .gitconfig file under the [alias] section, or by running a Git command like this:

git config --global alias.ds 'diff --staged'

Now, running git ds would execute git diff --staged.

Conclusion

With git diff, you have a powerful tool for inspecting changes in your codebase. Whether you’re looking at unstaged changes, a comparison between commits, or a difference between branches, git diff has the flexibility to provide you the details you need to understand and manage your changes.