Skip to content

Git Diff Tutorial

Overview

The git diff command is used to show changes between commits, commit and working tree, etc. It is a versatile command that can compare branches, commits, and changes in the working directory. This document will cover various usages of git diff with examples.

Table of Contents

  1. Basic Syntax
  2. Comparing Working Directory and Index
  3. Comparing Staged Changes
  4. Comparing Working Directory and Last Commit
  5. Comparing Between Commits
  6. Comparing Branches
  7. Diffing Specific Files
  8. Diffing with Different Output Formats
  9. Ignoring Whitespace Changes
  10. Useful Tips

Basic Syntax

The basic syntax of git diff is:

git diff [options] [<commit>] [<commit>]

Comparing Working Directory and Index

To see the changes that you have made but not yet staged, use:

git diff

Example

git diff

This will show the changes in the working directory that are not staged for the next commit.

Comparing Staged Changes

To see the changes that are staged for the next commit, use:

git diff --cached

Example

git diff --cached

This will show the changes that have been staged but not yet committed.

Comparing Working Directory and Last Commit

To see the changes between the working directory and the last commit, use:

git diff HEAD

Example

git diff HEAD

This will show the changes in the working directory compared to the latest commit.

Comparing Between Commits

To see the changes between two commits, use:

git diff <commit1> <commit2>

Example

git diff commit1 commit2

This will show the changes between commit1 and commit2.

Comparing Branches

To see the changes between two branches, use:

git diff <branch1> <branch2>

Example

git diff main feature-branch

This will show the differences between the main branch and the feature-branch.

Diffing Specific Files

To see the changes for a specific file, use:

git diff <file>

Example

git diff README.md

This will show the changes in README.md.

Diffing with Different Output Formats

git diff supports different output formats. The most common are:

  • Patch format (default): Shows the changes as a patch.
  • Name-only format: Shows the names of the changed files.
  • Name-status format: Shows the names and statuses (added, modified, deleted) of the changed files.

Examples

Patch Format

git diff

Name-Only Format

git diff --name-only

Name-Status Format

git diff --name-status

Ignoring Whitespace Changes

To ignore whitespace changes, use:

git diff -w

Example

git diff -w

This will ignore whitespace changes in the diff output.

Useful Tips

  • Color Diff Output: Use --color to force color diff output:

    git diff --color
    
  • Word Diff: Use --word-diff to show changes word by word:

    git diff --word-diff
    
  • Context Lines: Use -U<n> to show <n> lines of context around changes:

    git diff -U3
    

Summary

The git diff command is an essential tool for understanding changes in your Git repository. By using the various options and formats available, you can tailor the output to your needs and gain better insight into the differences between commits, branches, and your working directory.