Global PEP8 Git pre-commit check hook

If someone did a quick study to see how many Git commits were for forgetting to check PEP8 compliance before pushing, I bet it would be on the order of 3% as an initial guess. This clutter can be eliminated by making a simple Git pre-commit hook that applies to all repositories.


This procedure is for Unix-like systems (Linux, MacOS) and native Windows. The script used is written in Python to make it OS agnostic.

Tell Git the directory for Git hooks:

git config --global core.hooksPath ~/.git/hooks

Create executable file ~/.git/hooks/pre-commit from this script (without the .py in the filename).

This global pre-commit check hook does three things:

  1. get a list of Python file names that changed.
  2. check PEP8, mypy type hinting and that Python ≥ 3.7 breakpoint debug statements are not present.
  3. Checks for trailing whitespaces in any code language.

One can easily extend the concept of lint-checking for other programming languages.

Bypass checks

These checks can be bypassed at any time for a commit by:

git commit -n