Git hook
A script Git runs automatically at a point in its workflow, such as pre-commit, which can stop the commit by exiting non-zero.
In more detail
Hooks live in .git/hooks and are named after their event; the commit-msg hook gets the path of the message file and rejects the commit by exiting with a non-zero status. Because .git is never committed, a clone brings no hooks with it; core.hooksPath can point at a tracked folder instead. Anyone can skip them with --no-verify, so checks a team relies on belong in continuous integration.
Example
A pre-commit hook regenerates API clients from the OpenAPI document so generated code is never stale.Related terms
- secret scanning — Searching code, commits and files for credentials such as keys and tokens, ideally before they are committed.
- shift left — Doing testing, quality and security checks as early as they work reliably in the software development lifecycle, instead of only before release.
- exit code — A number a process returns when it finishes: 0 means success, anything else means something went wrong.
- code generation — Producing client or server code automatically from a machine-readable contract such as an OpenAPI document or a Protocol Buffers file.
- core.hooksPath — The setting that tells Git to look for hooks in a folder you choose instead of .git/hooks.
- --no-verify — A git commit and git push flag that skips the pre-commit, commit-msg and pre-push hooks.
- continuous integration — Automatically building and testing every change as soon as it is pushed, so problems are caught early.
- shell script — A text file of shell commands that runs them in order, top to bottom, as if you had typed them one by one.
- pre-commit hook — A Git hook that runs before a commit is created and can refuse the commit, for example when it contains something shaped like a secret.
Taught in
- Shift-Left Security Principles & Automated Pipeline Gates — Cloud Security & DevSecOps Engineering
- Pre-Commit Secret Scanning with Gitleaks & Entropy — Cloud Security & DevSecOps Engineering
- Type-Safe Client Generation: Orval & Automated SDKs — Production Full-Stack Engineering
- Conventional Commits — Advanced Git Internals & Distributed Workflows
- Why Secrets Leak — Application Security & Threat Modeling for Engineers
- Secret Scanning — Application Security & Threat Modeling for Engineers
- How To Stop Drowning In Findings — Application Security & Threat Modeling for Engineers