Pre-commit Hooks
Pre-commit hooks are scripts that run automatically on a developer's local machine before a code commit is finalized in a version control system. They check code for issues such as formatting errors, syntax problems, or policy violations before the commit is recorded. This gives developers fast, local feedback and helps prevent common mistakes from entering the codebase.
Pre-commit hooks are a category of Git hooks that execute as the first stage of the commit workflow, prior to the commit message prompt, on the developer's local working environment. They are typically implemented as shell scripts or managed through a framework such as pre-commit, which standardizes hook installation and execution across a team. Hooks at this stage can perform static analysis, linting, secret detection, and formatting validation against staged file content. Because they operate on local, pre-committed code rather than in a deployment or runtime context, their detection scope is limited to issues that are statically identifiable in source files; they cannot detect runtime behavior, environment-specific vulnerabilities, or issues that only manifest during execution. False negatives are possible when hooks are bypassed using the "--no-verify" flag, when hook configurations are incomplete, or when a check lacks coverage for a given file type or pattern. False positives may occur with overly strict linting or formatting rules. Pre-commit hooks complement but do not replace CI pipeline checks, as CI enforcement applies uniformly regardless of local hook configuration.
Why it matters
Pre-commit hooks shift security and quality checks as far left as possible in the development lifecycle, giving developers immediate feedback on their local machine before problematic code ever enters the shared repository. This early intervention reduces the cost of remediation: catching a formatting violation, exposed credential, or syntax error at commit time is faster and cheaper to fix than discovering it after it has been merged, reviewed, or deployed. Without local hooks, developers rely entirely on CI pipelines or code review to surface issues that could have been resolved in seconds during development.
Who it's relevant to
Inside Pre-commit Hooks
Common questions
Answers to the questions practitioners most commonly ask about Pre-commit Hooks.