How to Set Up Pre-Commit Hooks for Code Quality

Pre-commit hooks catch code quality issues before they're even committed, providing the fastest possible feedback loop — earlier and cheaper to fix than issues caught later in CI. This guide covers setting them up effectively.

Why Pre-Commit Is Faster Feedback Than CI

A CI pipeline check happens after you've already pushed code, potentially minutes later; a pre-commit hook runs locally in seconds, catching issues before they even leave your machine — complementary to, not a replacement for, CI-level checks (see How to Set Up Automated Testing in a CI Pipeline).

Installing the pre-commit Framework

pip install pre-commit --break-system-packages

Creating a .pre-commit-config.yaml

repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.5.0
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer
      - id: check-yaml
      - id: check-added-large-files

Installing the Hooks into Your Git Repository

pre-commit install

Now these checks run automatically before every commit, blocking the commit if any check fails.

Adding a Linter Hook

  - repo: https://github.com/psf/black
    rev: 24.1.1
    hooks:
      - id: black

Automated code formatting (here, Python's Black formatter) as a pre-commit hook ensures consistent code style is enforced automatically, rather than relying on manual reviewer feedback for style issues.

Adding a Secrets Detection Hook

  - repo: https://github.com/Yelp/detect-secrets
    rev: v1.4.0
    hooks:
      - id: detect-secrets

See How to Secure API Keys and Prevent Credential Leakage — catching accidentally-included secrets at commit time, before they ever reach version control history, is far preferable to discovering and remediating an exposure after the fact.

Running Hooks Manually Against All Files

pre-commit run --all-files

Useful when first adopting pre-commit on an existing repository, to check/fix existing files rather than only newly-committed ones.

Balancing Hook Comprehensiveness with Speed

Pre-commit hooks should be fast — a slow pre-commit process (running the full test suite, for example) frustrates developers and gets bypassed; reserve genuinely slow checks for CI, keeping pre-commit hooks focused on fast, high-value checks (formatting, linting, secrets detection, basic syntax validation).

Handling Team Adoption

Ensure all team members actually install the hooks (pre-commit install) — consider documenting this in your project's onboarding/README, and potentially enforcing the same checks in CI as a backstop for anyone who bypasses or hasn't installed local hooks.

Allowing Deliberate Bypass When Genuinely Necessary

git commit --no-verify -m "Emergency hotfix"

Occasionally a genuine emergency warrants bypassing hooks — understand this exists, but treat it as a rare exception, not a routine habit that undermines the value of having hooks in the first place.

Common Errors

Hooks installed but not actually running on commit — verify pre-commit install was actually run in this specific repository clone; the hook installation is per-clone, not automatically inherited from a cloned repository's configuration file alone.

Continue Reading

Browse more articles in DevOps & CI/CD.

  • pre-commit hooks setup, code quality automation, pre-commit-config.yaml example, git commit linting
  • 0 Kunder som kunne bruge dette svar
Hjalp dette svar dig?

Relaterede artikler

How to Set Up a Self-Hosted GitHub Actions Runner on a VPS

GitHub Actions' hosted runners work well for most projects, but a self-hosted runner on your own...

How to Deploy Automatically on Git Push (Webhook-Based Deployment)

Automating deployment whenever you push to a specific branch removes the manual "SSH in and pull"...

How to Set Up Blue-Green Deployment on a VPS

Blue-green deployment runs two identical production environments — only one live at a time...

How to Use Ansible for Server Configuration Management

Ansible automates server configuration through simple, human-readable YAML files — letting...

Infrastructure as Code Basics: Managing VPS Config with Terraform

Terraform lets you define infrastructure (VPS instances, networks, DNS records) as code, applied...