Summary

EN: pre-commit is a language-agnostic framework for managing git hooks that enforces code quality checks before every commit. Rather than writing and maintaining raw shell scripts, developers define hooks as a YAML configuration file that references versioned, reusable plugins. It supports Python, JavaScript, Ruby, and more, running linters, formatters, and security scanners automatically.

ZH: pre-commit 是一個語言無關的 git hooks 管理框架,透過 YAML 設定檔引用版本化插件,在每次提交前自動執行程式碼品質檢查。相比手寫 shell scripts,它更易維護且可跨語言共用,支援 flake8、black、bandit、pytest 等常見工具。

Key Points

  • Install via pip install pre-commit, then pre-commit install in your repo
  • .pre-commit-config.yaml defines hooks by repo URL, revision (tag/SHA), and hook id
  • Hooks are language-agnostic: Python, JavaScript, Ruby, system commands all supported
  • Example stack: flake8 (linting) + black (formatting) + bandit (security) + pytest (tests)
  • pre-commit run --all-files runs all hooks manually without committing
  • Hooks are cached and versioned, ensuring reproducibility across team members

Insights

  • Versioning hooks by git tag or SHA prevents “it works on my machine” drift — the hook environment is pinned just like a dependency
  • The real value is reducing code-review noise: catching style and trivial bugs before they ever reach the reviewer’s eyes
  • Teams that adopt pre-commit report faster PRs because the automated checks handle the mechanical feedback, freeing reviewers for logic and design

Connections

  • Complements CI/CD pipelines (GitHub Actions, etc.) — pre-commit catches issues locally, CI catches what slips through
  • Related to the “shift left” testing philosophy: find bugs earlier in the development cycle
  • Works well alongside tools like commitlint or commitizen for commit message enforcement

Raw Excerpt

“pre-commit is a multi-language package manager for pre-commit hooks. You specify a list of hooks you want and pre-commit manages the installation and execution of any hook written in any language before every commit.”