Read Anthropic’s case study about Graphite Reviewer

How to ignore whitespace while using `git diff`

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


One common issue when using git diff is dealing with whitespace changes. While sometimes meaningful, white space changes can also clutter the diff output with irrelevant changes. This guide will explain how to use git diff to ignore whitespace, enhancing the clarity of the diffs you review.

Whitespace changes include alterations in spaces, tabs, and line breaks that, depending on the language you're using, don't always affect the actual execution of the code. These can be distracting in reviews with large formatting changes, where you want to solely focus on substantive code changes.

git diff provides several options to ignore changes solely related to whitespace, making the command output cleaner and more relevant.

  • Ignoring all whitespace changes:
    Terminal
    git diff -w
    The -w option is the most straightforward way to ignore whitespace. It disregards all white space when comparing lines.
  • Ignoring changes in spaces and tabs:

    Terminal
    git diff --ignore-space-change

    This option ignores changes in the amount of space and tab characters, while still displaying changes in new lines.

  • Ignoring whitespace at the end of lines:

    Terminal
    git diff --ignore-space-at-eol

    This option focuses on ignoring spaces at the end of lines, which is particularly useful in languages where end-line whitespace is irrelevant.

  • Ignoring changes whose lines are all blank:
    Terminal
    git diff --ignore-blank-lines
    This option will skip diffs that are purely blank line changes.

Here are a few practical examples to demonstrate how these options can be used in real scenarios:

  1. Compare two branches without whitespace:

    Terminal
    git diff -w branch1..branch2

    This will show differences between branch1 and branch2, ignoring all whitespace differences.

  2. Review staged changes ignoring end-of-line whitespace:

    Terminal
    git diff --cached --ignore-space-at-eol

    Useful for a final review before committing, this command ignores whitespace at the end of lines in the staged files.

  3. Diff a specific file, ignoring changes in the amount of whitespace:

    Terminal
    git diff -b -- path/to/file

    This command focuses on substantive changes, ignoring spaces and tabs within lines for the specified file.

While care should always be taken to follow your organizations style guide regarding whitespace, using the git diff command to temporarily ignore whitespace changes in order to focus specifically on the code's logic and functionality.

For further reading on the different applications of the git diff command, see the official Git documentation.

Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2