Manual Setup
-
Create the hook file at
.git/hooks/pre-commit: -
Make it executable:
-
Test it:
Using Husky
-
Install Husky:
-
Add the pre-commit hook:
-
Commit the hook configuration:
Using Lefthook
-
Install Lefthook:
-
Configure
lefthook.yml: -
Initialize and commit:
Customizing Hook Behavior
All scanning commands accept flags to customize behavior. See the Commands reference for the full list. Common examples for hooks:How It Works
When you rungit commit:
- The pre-commit hook runs the configured scanning commands
- Each scanner analyzes your staged files (the
--stageddefault) - If issues are found above the
--fail-onthreshold:- The commit is blocked
- Issue locations and details are displayed
- Fix the issues, re-stage, and try again
- If no blocking issues are found:
- The commit proceeds normally
Bypassing Hooks
In rare emergencies, you can bypass pre-commit hooks:--no-verify in emergencies. Bypassing scans can allow secrets, vulnerabilities, or code quality issues into your repository.