Using Git Hooks for Code Quality
Git hooks are powerful tools that allow you to run scripts automatically when certain Git events occur. When used effectively, they help enforce code quality standards, catch issues early, and reduce errors before code reaches shared repositories.
1. What Are Git Hooks?
Git hooks are scripts that run automatically at specific points in the Git workflow, such as:
Before a commit
After a commit
Before a push
They live in the .git/hooks directory of a repository.
2. Why Use Git Hooks for Code Quality?
Git hooks help:
Prevent broken code from being committed
Enforce coding standards
Catch formatting and linting issues early
Reduce CI pipeline failures
Improve team-wide consistency
They act as the first line of defense for code quality.
3. Common Git Hooks for Code Quality
Pre-commit
Runs before a commit is created.
Typical uses:
Linting
Code formatting
Running unit tests
Checking for secrets
Commit-msg
Runs after the commit message is entered.
Typical uses:
Enforcing commit message conventions
Validating ticket references
Pre-push
Runs before code is pushed to a remote repository.
Typical uses:
Running full test suites
Preventing pushes to protected branches
4. Example: Pre-Commit Hook for Linting
Basic Shell Script
Create .git/hooks/pre-commit:
#!/bin/sh
npm run lint
if [ $? -ne 0 ]; then
echo "Linting failed. Commit aborted."
exit 1
fi
Make it executable:
chmod +x .git/hooks/pre-commit
5. Using Husky (Recommended for Teams)
Manual hooks are not version-controlled. Husky solves this by managing Git hooks through your project configuration.
Install Husky
npm install husky --save-dev
npx husky install
Add a Pre-Commit Hook
npx husky add .husky/pre-commit "npm run lint"
Example with Lint-Staged
Run checks only on staged files:
npm install lint-staged --save-dev
package.json:
{
"lint-staged": {
"*.{js,ts}": ["eslint --fix", "prettier --write"]
}
}
6. Common Code Quality Checks
Git hooks can enforce:
ESLint / Pylint rules
Prettier formatting
Unit tests
Type checking
Secret scanning
File size limits
7. Performance Best Practices
Keep hooks fast
Avoid running full test suites in pre-commit
Use pre-push for heavier checks
Run checks only on staged files
8. Handling Hook Failures
When a hook fails:
Provide clear error messages
Explain how to fix the issue
Avoid silent failures
Good feedback improves developer experience.
9. Git Hooks vs CI
Git hooks:
Run locally
Provide fast feedback
Can be bypassed (with --no-verify)
CI pipelines:
Enforce rules centrally
Cannot be bypassed
Handle heavy checks
Best practice: Use both.
10. Security Considerations
Never trust hooks alone for enforcement
Always back up checks in CI
Avoid running untrusted scripts
Conclusion
Git hooks are an effective way to improve code quality early in the development workflow. By automating linting, formatting, and basic tests, teams can catch issues before they reach code review or CI pipelines.
When combined with tools like Husky and lint-staged, Git hooks become a powerful and developer-friendly solution for maintaining clean, consistent codebases.
Learn MERN Stack Training in Hyderabad
Read More
Using Postman Collections for Testing
Using PM2 for Node Process Management
Visit Our Quality Thought Training Institute in Hyderabad
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments