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. For non-Python repositories, the flake8 command will quickly exit as it doesn’t find any .py files.

This example makes the following assumptions:

  • flake8 is installed and working e.g. pip install flake8
  • All the Python projects have the file at the top-level (to avoid running flake8 on large non-Python projects)


Create a directory to hold the Git hooks:

mkdir -p ~/.git/hooks

Tell Git this is a directory for Git hooks:

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

Create executable file ~/.git/hooks/pre-commit containing:


export pyfiles=$(git diff --staged --name-only | grep -E "\.py$")

if [[ ! -z $pyfiles ]]
grep -l 'breakpoint' $pyfiles && { echo "remove breakpoint before commit"; exit 1; }

exec flake8 $pyfiles

exec git diff-index --check --cached HEAD --

This global pre-commit check hook does three things:

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

Bypass checks

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

git commit --no-verify