← til

Using Git pre-push hook for linting

May 7, 2020
git

In the past, I've used Git pre-commit hooks for linting. This started to get annoying incredibly fast, since it meant I had to wait for linting to complete before every commit.

I amend commits pretty regularly, to either change the wording of the commit message or the contents, so this has significantly slowed me down.

After removing these hooks, I've observed that this project has suffered from a significant increase of CI failures due to linting errors. This is not surpising, since the automatic local checks were removed. Turns out automatic linting still has some benefits and my problem with it was running it too frequently.

Well, Git 1.8.2. has added another hook that can be used for this: the pre-push hook. This hook enables running commands before each push. All I needed to do to enable it was to edit .git/hooks/pre-push:

#!/bin/bash

./bin/ci_lint

After making this file exectuable with chmod +x, Git will run these linters before each push.