codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
📦

Git & GitHub

8 / 63 topics
1Installing Git2Configuring Git3Initializing a Repository4Using git status5Using git add6Using git commit7Using git log8Using git diff
Tutorials/Git & GitHub/Using git diff
📦Git & GitHub

Using git diff

Updated 2026-04-20
3 min read

Introduction

git diff is a powerful command-line tool that allows you to examine changes between different versions of files in your Git repository. It's an essential part of the version control workflow, enabling developers to understand what has changed, how it has changed, and why these changes might be necessary. This tutorial will guide you through using git diff effectively, providing real-world examples and best practices.

Understanding git diff

Before diving into specific commands, let's break down what git diff does:

  • Comparing Changes: It compares the differences between two commits, a commit and a file, or even between different branches.
  • Visualizing Differences: By default, it outputs the changes in a unified format that highlights additions (in green) and deletions (in red).
  • Customization: You can customize the output to suit your needs, such as showing only the names of changed files or limiting the number of lines shown.

Basic Usage

1. Comparing Staged vs Unstaged Changes

When you've made changes in your working directory but haven't staged them yet, you can see these changes using:

git diff

This command will show you the differences between the files in your working directory and the last commit.

2. Comparing Staged vs Last Commit

If you've staged some changes but not committed them yet, you can compare the staging area with the last commit using:

git diff --cached

or simply:

git diff --staged

This will show you the differences between what's in the index (staging area) and the most recent commit.

3. Comparing Two Commits

To compare changes between two specific commits, use:

git diff <commit1> <commit2>

For example, to see the differences between the last commit and the one before it, you can use:

git diff HEAD~1 HEAD

4. Comparing Branches

To compare changes between two branches, use:

git diff <branch1> <branch2>

For instance, to see what has changed on feature-branch compared to main, you would run:

git diff main feature-branch

Advanced Usage

1. Showing Only File Names

If you want to quickly see which files have changes without the actual content differences, use:

git diff --name-only

This is useful for getting a high-level overview of what has changed.

2. Limiting Output Lines

To limit the number of lines shown in the diff output, use the --unified option followed by the desired number of lines. For example:

git diff --unified=5

This will show only five lines of context around each change.

3. Ignoring Whitespace Changes

Sometimes, whitespace changes (like spaces vs tabs) can clutter your diff output. To ignore these changes, use:

git diff -w

or

git diff --ignore-all-space

4. Using External Diff Tools

If you prefer a graphical interface for viewing diffs, Git supports external diff tools like meld, kdiff3, or vimdiff. To configure an external tool, add the following to your .gitconfig:

[diff]
    tool = meld
[difftool "meld"]
    cmd = meld "$LOCAL" "$REMOTE"

Then, you can use:

git difftool <commit1> <commit2>

to open the diff in your configured tool.

Best Practices

  • Regularly Use git diff: Make it a habit to check changes before committing. This helps catch errors early and ensures that your commits are meaningful.

  • Review Changes Carefully: Always review the output of git diff thoroughly, especially when dealing with complex codebases or sensitive areas.

  • Use Contextual Information: When reviewing diffs, consider the context in which changes were made. Understanding why a change was necessary can provide valuable insights.

  • Customize Your Output: Tailor your git diff commands to suit your workflow. For example, if you frequently work with large files or need to ignore certain types of changes, configure your .gitconfig accordingly.

Conclusion

git diff is an indispensable tool for any developer working with Git. By mastering its various options and configurations, you can enhance your productivity and ensure that your codebase remains clean and well-managed. Whether you're reviewing changes before committing or comparing branches, git diff provides the insights you need to make informed decisions.

Remember, practice makes perfect. The more you use git diff, the more comfortable and efficient you'll become at identifying and understanding changes in your codebase.


PreviousUsing git logNext Branching and Merging

Recommended Gear

Using git logBranching and Merging