Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Configuration Options

core.whitespace setting

Reference for core.whitespace: choose which whitespace problems git diff --check, git apply and colored diffs flag, from trailing-space to tab-in-indent.

core.whitespace is a comma-separated list that decides which whitespace patterns Git counts as errors. Those errors are highlighted in red in git diff, reported by git diff --check, and warned about or fixed by git apply and git am with --whitespace=warn or fix.

By default Git treats three things as errors: spaces at the end of a line (blank-at-eol), blank lines added at the end of a file (blank-at-eof), and a space immediately before a tab in the indentation (space-before-tab). Everything else is opt-in.

Terminal
git config core.whitespace "trailing-space,space-before-tab,tab-in-indent"
git config core.whitespace "-blank-at-eol"     # a leading minus turns a default rule off
git diff --check                                # fails with a status code if errors exist

Try it

Bash Finding whitespace errors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Banning tabs and fixing on apply

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
git config core.whitespace <rule>[,<rule>...]
Rule Meaning
blank-at-eol Spaces or tabs at the end of a line. On by default.
blank-at-eof Blank lines added at the end of the file. On by default.
space-before-tab A space before a tab in the initial indent. On by default.
indent-with-non-tab An indent of eight or more spaces (the width is tabwidth).
tab-in-indent Any tab in the indentation. Use it for space-only projects.
cr-at-eol Treat a carriage return before the newline as part of the line, not an error.
trailing-space Shorthand for blank-at-eol plus blank-at-eof.
tabwidth=<n> Tab width used by indent-with-non-tab (default 8).

Prefix a rule with - to disable it. The same list can be set per path with the whitespace attribute in .gitattributes.

Returns: git diff --check prints file:line: description. followed by the offending line, and exits with status 2 when problems exist.

Usage notes

  • Only added lines count. Existing whitespace problems in unchanged lines are not reported.
  • Great in pre-commit: run git diff --cached --check in a hook to stop whitespace noise before it enters history.
  • Windows files: with CRLF endings every line has a \r before the newline; add cr-at-eol (or let core.autocrlf convert the endings) to avoid a flood of "trailing whitespace" reports.
  • git apply --whitespace accepts nowarn, warn, fix and error; git am passes it through. fix rewrites the patch as it is applied.
  • Color: error highlights use color.diff.whitespace.
  • git log --check applies the same rules to each commit in a range.
  • Markdown exception: two trailing spaces are a hard line break in Markdown, so disable blank-at-eol for *.md via an attribute.