TIL: Configuring pre-commit Hooks for Python and Node Repos
Every team has that one member who forgets to lint before pushing. pre-commit automates it.
# .pre-commit-config.yaml
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- repo: https://github.com/psf/black
rev: 22.8.0
hooks:
- id: black
Install and activate.
pip install pre-commit
pre-commit install
Now every git commit runs the hooks. If trailing whitespace exists, the commit fails. Fix the issue, stage the fix, commit again. Clean repo, no reminders.
pre-commit run --all-files
Run all hooks against every file in the repo. Useful for CI checks or cleaning up a legacy codebase.
Does pre-commit work with Node projects?
Yes. Add eslint or prettier hooks using the same repo URL pattern. The hooks are language agnostic.