A pre-commit Baseline That Actually Runs: Config, Auto-Fix, and CI Enforcement

Most pre-commit setups are decorative. A .pre-commit-config.yaml sits in the repository, it runs on the author’s machine when they remember to install it, and it is silently bypassed the moment anyone is in a hurry. That is not a control. A control is something that holds when the person it applies to would rather it didn’t.

The pre-commit framework is genuinely good software. The problem is almost never the tool; it is that teams treat the local hook as the enforcement point. It isn’t. A local hook is a convenience for the developer who installed it. Anyone can skip it with git commit --no-verify, and anyone who cloned the repository without running pre-commit install never had it in the first place. If your formatting and linting standard only exists on the machines of people who opted in, you do not have a standard. You have a suggestion.

This is a setup guide for a baseline that actually runs: a pinned config that auto-fixes what it safely can, and a continuous integration job that re-runs the same hooks on every pull request so nothing reaches the main branch unformatted, whatever happened locally. It is deliberately small. You can extend it later; the point here is a floor that holds.

Free · 4 minutes

Is your engineering team shipping safely, or quietly accumulating risk?

Fourteen questions on how work gets from idea to production — cadence, testing, rollback, and the key-person risk in your delivery. Banded finding on screen, full sheet by email.

The config, with versions pinned

Here is a .pre-commit-config.yaml that wires the Astral ruff linter and formatter for Python, prettier for the JavaScript, JSON, YAML and Markdown around it, and a handful of the built-in hooks that catch the errors nobody means to commit. Every rev is pinned to an exact tag, which matters more than it looks.

# .pre-commit-config.yaml
repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v6.0.0
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer
      - id: check-merge-conflict
      - id: detect-private-key
      - id: check-yaml
      - id: check-added-large-files

  - repo: https://github.com/astral-sh/ruff-pre-commit
    rev: v0.15.17
    hooks:
      # Lint first, and let ruff fix what it safely can.
      - id: ruff-check
        args: [--fix]
      # Then format, so the formatter has the last word.
      - id: ruff-format

  # The original pre-commit/mirrors-prettier is archived;
  # rbubley/mirrors-prettier is the maintained replacement.
  - repo: https://github.com/rbubley/mirrors-prettier
    rev: v3.6.2
    hooks:
      - id: prettier

Two details are load-bearing. The first is the ordering inside the ruff-pre-commit block: ruff-check with --fix runs before ruff-format, so the linter’s auto-fixes are then re-formatted rather than fighting the formatter for the last word. The hook is ruff-check, not the older bare ruff id, which Astral has since renamed. The second is detect-private-key. It is a blunt instrument and it will not catch everything, but it is free, and a committed private key is the kind of mistake that ends up in an incident report. It pairs naturally with treating secrets as first-class material rather than strings in a file, which is a discipline we have written about in the context of API keys that aren’t a liability.

Pinning to an exact rev is not pedantry. An unpinned or floating hook means the version that runs on your laptop, on your colleague’s laptop and in CI can silently diverge, and a formatter that disagrees with itself across machines produces churn that looks like real changes in every diff. Pin the tag, and bump it deliberately with pre-commit autoupdate when you choose to, as a reviewed change like any other.

Install, and what auto-fix actually does

Installing the framework and registering the git hook is two commands, and a third to shake the config down against the whole tree the first time:

pip install pre-commit
pre-commit install
pre-commit run --all-files

pre-commit install writes a hook into .git/hooks/pre-commit for that clone only. That is the crux of the local model: it is per-clone, opt-in, and invisible to anyone who skipped it. The first run --all-files will almost certainly reformat a pile of files, which is expected — commit that sweep on its own so the substantive history stays clean.

From then on, on a normal commit the hooks run against staged files only. When a formatter or ruff --fix changes a file, the commit is aborted and the fixes are left in your working tree, unstaged. This trips people up the first time: pre-commit does not stage the fixes for you and re-commit. You review the change, git add it, and commit again. That extra step is the tool refusing to put unreviewed edits into your commit on your behalf, which is the correct default.

The CI job that makes it a gate

Everything above is a convenience until it runs somewhere the developer cannot bypass. That place is CI. The following GitHub Actions workflow runs the identical hook set on every pull request and every push to main, so a branch that was committed with --no-verify, or by someone who never installed the hook, still fails the check.

# .github/workflows/pre-commit.yml
name: pre-commit
on:
  pull_request:
  push:
    branches: [main]

jobs:
  pre-commit:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
      - uses: actions/cache@v4
        with:
          path: ~/.cache/pre-commit
          key: pre-commit-${{ hashFiles('.pre-commit-config.yaml') }}
      - run: pip install pre-commit
      # --all-files checks the whole tree, not just the diff.
      # --show-diff-on-failure prints what would change, so the
      # failure is actionable rather than just red.
      - run: pre-commit run --all-files --show-diff-on-failure

Because pre-commit provisions its own tool environments — including the Node runtime prettier needs — you do not set up Node separately; the framework builds and caches each hook’s environment itself. The actions/cache step keyed on the hash of the config file is what stops that provisioning from happening from scratch on every run. Make this workflow a required status check in the branch protection rules, and the gate is real: the main branch cannot take a change that fails the hooks, regardless of what happened on anyone’s laptop.

That is also the point at which formatting stops being a matter of taste and becomes part of change governance. The hooks are a small, legible, version-controlled statement of what “acceptable” means, enforced automatically at the boundary — the same instinct that underlies expressing controls as code rather than as prose in a policy document nobody reads.

Why the CI job is the whole point

Keep the local and CI configurations identical — same file, same pins — so a developer never sees a green commit locally and a red check remotely for reasons they cannot reproduce. When those two diverge, people stop trusting the local hook, and a hook nobody trusts gets bypassed as a matter of routine.

There is a metrics angle to this too. A gate that quietly rejects malformed changes before they merge is one of the cheapest ways to hold your change-failure rate down, which is worth measuring deliberately rather than assuming — the discipline behind instrumenting the DORA four.

The local hook is for the developer. The CI job is for the organisation. Ship both, but if you only have time to wire up one, wire up the one that runs whether or not anyone asked it to.

Free interactive tool

Website compliance checklist

What your site has to do, based on what it actually does

Answer as much or as little as you like — the list builds as you go. Nothing is stored against your name and no email is required.

Most technology problems are not technology problems. They are control problems.

The systems exist. The investment has been made. The question is whether leadership can understand, direct, evidence, and sustain what those systems produce. Find out where control exists — and where it only appears to.