Last Updated: February 25, 2016
·
633
· teddyzetterlund

No whitespace in Git repositories

You can find whitespace with the help of Git with the following command: git diff-index --check --cached.

You can have an automatic check for this by enabling a Git pre-commit hook ([your-project]/.git/hooks/pre-commit) with this code:

#!/bin/sh

if git rev-parse --verify HEAD >/dev/null 2>&1
then
  against=HEAD
else
  # Initial commit: diff against an empty tree object
  against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

You can read more about this in my blog post Fight whitespace with Git