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.
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
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.
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
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: rungit diff --cached --checkin a hook to stop whitespace noise before it enters history. - Windows files: with CRLF endings every line has a
\rbefore the newline; addcr-at-eol(or letcore.autocrlfconvert the endings) to avoid a flood of "trailing whitespace" reports. git apply --whitespaceacceptsnowarn,warn,fixanderror;git ampasses it through.fixrewrites the patch as it is applied.- Color: error highlights use
color.diff.whitespace. git log --checkapplies 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-eolfor*.mdvia an attribute.
Related
core.autocrlf,git diff,git apply,color.*- Lessons: Status and diff, Hooks