How to ignore whitespace while using `git diff`

Greg Foster
Greg Foster
Graphite software engineer

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:
    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:

    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:

    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:
    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:

    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:

    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:

    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.

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Get started

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2