How to Use Git Diff to Ignore Whitespaces in Git
John Wachira
Feb 02, 2024
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.
- We use the
git diff -w
command to ignore all whitespace differences. It will ignore spaces at the beginning, middle, and end of lines. - 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 thegit diff
output. - The
git diff --b
is the short form for thegit diff --ignore-space-change
that ignores whitespace at the line end. The command considers other sequences of one or multiple whitespaces equivalent. - 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
. - We can use the
git diff --ignore-blank-lines
to ignore the changes where the lines are blank.
Author: John Wachira
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.
LinkedInRelated Article - Git Diff
- How to Show Diff Details for Uncommitted Changes in Git
- How to Diff a File to an Arbitrary Version in Git
- How to Set Up Meld as Difftool and Mergetool for Git
- How to Set Up SourceGear DiffMerge Tool for Git
- How to Show Staged and Unstaged Changes in Git
- How to Compare Files, Commits, and Branches in Git