6

I have a markdown file with lines that have trailing whitespace (which is correct and should be commited). I'm unable to add these changes using git add -p to the index because git complains about trailing whitespace. They are added correctly if I use git add -A, but I want it to work with git add -p.

I have in my ~/.gitconfig:

[core]
  whitespace = trailing-space,space-before-tab

This has been working fine since for the most part I DO want to warn on trailing whitespace (it is incorrect in HTML, JS and Ruby files).

How do I ignore the trailing whitespace in Markdown files only?

Devon Parsons
  • 1,234
  • 14
  • 23

2 Answers2

8

In a .gitattributes file add the following:

*.md  -whitespace

https://git-scm.com/docs/gitattributes#_checking_whitespace_errors

More specifically you could instead do the following:

*.md  whitespace=space-before-tab

(dropping the trailing-space for markdown files.)

Treat .gitattributes in the same way you do .gitignore and check it into the repo.

Sukima
  • 9,965
  • 3
  • 46
  • 60
  • What does `**/*` mean? `-whitespace` means "unset the `whitespace` attribute". – endolith Jan 06 '22 at 16:54
  • `**/` is a wildcard to match files in current folder and any descendants. – Ian Yang Oct 01 '22 at 22:19
  • By the way, it's possible to add a global gitattributes file “Attributes that should affect all repositories for a single user should be placed in a file specified by the core.attributesFile configuration option (see git-config[1]).” – Ian Yang Oct 01 '22 at 22:21
  • 1
    `*.md` and `**/*.md` are equivalent for .gitattributes, because attributes only apply to files (not directories). – rjh Nov 03 '22 at 19:57
  • Using git version 2.34.1 the first suggestion (`-whitespace`) worked for me, but the second (`whitespace=space-before-tab`) did not. – mactyr Mar 15 '23 at 20:33
  • `**/` is unnecessary and ugly. – lzhh Jul 05 '23 at 01:08
2

Use this in .gitattributes:

**/*.md text whitespace=-cr-at-eol,-trailing-space

**/*.md whitespace=space-before-tab does not work. Try this in cmd.exe:

$ git config --show-origin --get core.whitespace
file:C:/Users/kevin/.gitconfig  trailing-space,space-before-tab,cr-at-eol

$ git init .
Initialized empty Git repository in trailing/.git/

$ cat > README.md
Trailing space here:
check it

$ git add README.md

$ git diff-index --check --cached 4b825dc642cb6eb9a060e54bf8d69288fbee4904
README.md:1: trailing whitespace.
+Trailing space here: 

$ echo **/*.md  -whitespace > .gitattributes

$ git check-attr --all -- README.md
README.md: whitespace: unset

$ git diff-index --check --cached 4b825dc642cb6eb9a060e54bf8d69288fbee4904

$ echo **/*.md  whitespace=space-before-tab > .gitattributes

$ git check-attr --all -- README.md
README.md: whitespace: space-before-tab

$ git diff-index --check --cached 4b825dc642cb6eb9a060e54bf8d69288fbee4904
README.md:1: trailing whitespace.
+Trailing space here:

$ echo **/*.md text whitespace=-cr-at-eol,-trailing-space > .gitattributes

$ git diff-index --check --cached 4b825dc642cb6eb9a060e54bf8d69288fbee4904
Kevin Smyth
  • 1,892
  • 21
  • 22