A reliable code-quality workflow does more than run a formatter on a developer’s machine. This guide shows how to combine formatting, linting, type checking, tests, Git hooks, and CI so contributors receive fast local feedback while the main branch remains protected by an independent server-side check.
Overview
Teams often ask, “How do I make code-quality checks run consistently before code reaches the main branch?” The practical answer is layered enforcement. Local hooks make the preferred path convenient, pre-push checks catch problems before they leave a workstation, and CI provides the final authority because local hooks can be skipped, misconfigured, or unavailable.
Use the following division of responsibility:
- Formatting: Make source files follow one reproducible style. Formatting should usually be automatic and produce minimal debate.
- Linting: Detect suspicious patterns, likely defects, and project-specific rule violations.
- Type checking: Validate contracts between values, functions, modules, or services where the language supports it.
- Tests: Check behavior, with fast tests locally and the complete appropriate suite in CI.
- Git hooks: Provide early feedback without replacing CI enforcement.
- CI: Re-run authoritative checks in a clean, repeatable environment and report results on the pull request.
Keep the local path short enough that contributors will use it. A common pattern is to format and lint staged files before a commit, then run type checks and a fast test suite before push. CI can run the same commands against the full repository. This reduces the chance that a developer’s machine, editor, or operating system changes the result.
Checklist by scenario
1. Establish one command for each check
Start by making quality checks explicit and runnable without an editor. Add scripts or equivalent task definitions to the repository so contributors and CI invoke the same interface. Names should describe intent rather than implementation:
{
"scripts": {
"format": "formatter .",
"format:check": "formatter --check .",
"lint": "linter .",
"typecheck": "typechecker --noEmit",
"test:unit": "test-runner",
"verify": "package-manager run format:check && package-manager run lint && package-manager run typecheck && package-manager run test:unit"
}
}
The command names above are placeholders. Replace them with the formatter, linter, type checker, and test runner used by the project. The important convention is that a fresh checkout can run the checks without relying on undocumented local settings.
2. Configure formatting before stricter lint rules
Choose a formatter configuration and commit it to the repository. Decide whether generated files, vendor directories, build output, and snapshots should be excluded. Use a check-only mode in CI so the pipeline reports a failure rather than silently changing files.
- Commit the formatter configuration and ignore file.
- Document the supported runtime and package-manager commands.
- Run formatting on a deliberate baseline rather than mixing unrelated refactoring into the first change.
- Use the same line-ending and character-encoding assumptions across local and CI environments.
3. Add linting, type checking, and tests in a deliberate order
Linting should explain the rule that failed and, where safe, offer an automatic fix. Type checking should use the project’s committed configuration rather than editor defaults. Tests should be separated by speed or scope when that helps contributors get feedback quickly.
A useful local sequence is:
format changed files
lint changed files
run type checks
run fast tests
For a repository with multiple languages, give each tool a clear scope. A monorepo may have package-level commands plus a root command that coordinates them. Teams using a multirepo model can apply the same contract in each repository; the choice between those layouts is discussed in this monorepo versus multirepo decision guide.
4. Install commit and pre-push hooks
Git hooks live in a developer’s local repository and are not automatically shared merely because they exist in one checkout. Use a committed hook-management approach that installs hooks after a dependency setup step, or document an explicit installation command for contributors.
A commit hook should normally handle quick, high-signal work such as formatting and linting staged files. A pre-push hook can run type checks and a fast test suite, but avoid placing a slow integration suite in every push unless the team has accepted that trade-off.
For a simple native setup, a repository can point Git at a versioned hooks directory:
git config core.hooksPath .githooks
chmod +x .githooks/pre-commit .githooks/pre-push
Other teams use a dedicated hook manager. Common options include a framework that reads a repository configuration, a JavaScript hook manager paired with staged-file filtering, or a language-neutral hook runner. Compare them by installation behavior, support for multiple languages, staged-file handling, Windows compatibility, debugging, and maintenance overhead rather than by popularity alone.
| Approach | Good fit | Double-check |
|---|---|---|
| Native Git hooks | Small projects with few dependencies | Installation, executable permissions, and cross-platform shell behavior |
| Pre-commit-style framework | Repositories with several languages and shared hook definitions | Environment creation, tool versions, and first-run cost |
| Language ecosystem manager | Projects already centered on one package ecosystem | Monorepo behavior, staged files, and onboarding commands |
| General hook runner | Teams needing explicit ordering and multiple task types | Configuration complexity and how failures are displayed |
5. Enforce the same contract in CI
CI should install dependencies from the lockfile, use a declared runtime, and run non-mutating checks. A generic pipeline looks like this:
steps:
- checkout repository
- install the declared runtime
- install dependencies from the lockfile
- run format:check
- run lint
- run typecheck
- run fast tests
- run integration or end-to-end tests when required
Protect the main branch with required status checks or the equivalent controls provided by your Git hosting system. Local hooks improve behavior; only a server-side rule can consistently stop an unverified change from being merged. If you operate your own runners, review the self-hosted Git runner security checklist before granting jobs access to sensitive systems.
6. Add a team policy for exceptions
Write down how the team handles existing violations, generated code, flaky tests, and urgent fixes. A practical policy template is:
- New or modified code must pass the repository’s required formatter, linter, type, and test checks.
- Legacy violations may be tracked separately and should not be expanded by unrelated changes.
- Generated files are checked according to their generator’s documented workflow, not manually reformatted without review.
- An emergency bypass requires an explanation, an owner, and a follow-up issue or change when the bypass is safe to remove.
- Bypassing a local hook is allowed only when the contributor understands that CI remains authoritative.
What to double-check
- Version parity: Confirm that local and CI use compatible runtime, formatter, linter, and test-tool versions. Pin or constrain versions where reproducibility matters.
- Dependency installation: Use the lockfile in CI and fail clearly when it is out of date.
- Changed-file behavior: Verify that staged-file tools do not accidentally skip renamed files, files in nested packages, or files with spaces in their names.
- Exit codes: A formatter’s write mode and check mode are different. Ensure CI fails when formatting is wrong.
- Generated and ignored files: Confirm that the formatter, linter, test runner, and CI artifact collection agree about what belongs in the repository.
- Parallel jobs: If checks run concurrently, make failures easy to identify and avoid duplicate setup that creates inconsistent environments.
- Secrets and untrusted code: Pull requests from outside contributors should not receive unnecessary credentials, particularly on self-hosted runners.
- Branch protection: Check that required jobs refer to stable job names and cannot be satisfied by an unrelated or skipped workflow.
When changing hosting or CI providers, include hooks and required status checks in the migration plan. The Git hosting migration checklist can help identify workflow dependencies that are easy to overlook.
Common mistakes
- Relying only on local hooks: Hooks can be skipped, deleted, or never installed. Keep CI as the enforcement layer.
- Running everything on every commit: Slow hooks encourage bypasses. Put the fastest useful checks before commit and reserve broader checks for pre-push or CI.
- Changing formatting and behavior together: A large formatting diff makes review and conflict resolution harder. Separate baseline formatting when possible.
- Using different commands locally and in CI: This creates “works on my machine” failures. Expose one repository-defined command for each check.
- Auto-fixing without review: Let tools modify files locally, but make the resulting diff visible and require the contributor to inspect it.
- Making legacy cleanup a prerequisite for every change: Track old violations separately and improve the touched area incrementally.
- Hiding failures behind a wrapper: A script that always exits successfully defeats quality gates. Test failure paths as well as successful runs.
When to revisit
Review this workflow before seasonal planning cycles and whenever the repository’s inputs change. Revisit it when adding a programming language, adopting a monorepo, changing package managers, upgrading a formatter or linter, moving CI providers, introducing self-hosted runners, or changing branch protection rules.
Use this short review checklist:
- Clone the repository into a clean environment and follow the documented setup.
- Run the documented format, lint, type, and test commands.
- Create a deliberately failing change and confirm that the local hook and CI report it clearly.
- Confirm that a formatting-only change is detected by the CI check mode.
- Test the documented emergency-bypass process and verify that it leaves an auditable explanation.
- Remove obsolete rules, scripts, and exceptions instead of allowing them to accumulate.
Finally, record the workflow in the repository’s contributor documentation and onboarding steps. A quality system works best when it is predictable: contributors know what runs, maintainers know what is required, and the main branch is protected by checks that do not depend on any one person’s local setup.