How to Use Git Diff to Ignore Whitespaces in Git

John Wachira Feb 02, 2024
How to Use Git Diff to Ignore Whitespaces in Git

This article will discuss how you can ignore whitespaces using the git diff command. We use git diff to compare commits, branches, and files across branches or revisions.

Use the git diff Command to Ignore Whitespaces in Git

In a scenario where we have a file with 700 lines of code, and we make changes from tabs to spaces in more than 400 lines, our view will have several git diffs resulting from our change.

We can ignore the whitespaces using the following options.

  1. We use the git diff -w command to ignore all whitespace differences. It will ignore spaces at the beginning, middle, and end of lines.
  2. We use the git diff --ignore-space-at-eol command to ignore whitespace changes at the end of our lines. Whitespaces at the beginning and in the middle of lines will remain in the git diff output.
  3. The git diff --b is the short form for the git diff --ignore-space-change that ignores whitespace at the line end. The command considers other sequences of one or multiple whitespaces equivalent.
  4. Note that there is no built-in solution for ignoring white spaces for the start line only. You can use old patches that support git diff --ignore-space-at-sol.
  5. We can use the git diff --ignore-blank-lines to ignore the changes where the lines are blank.
Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Diff