Pre-Push Hook Guide
Enhanced pre-push validation system that runs comprehensive checks locally to catch issues before pushing to the repository, ensuring high code quality and reducing CI failures.
âšī¸ Info: Local Validation: Pre-push hooks run the same checks as CI locally, providing immediate feedback and preventing broken code from reaching the repository.
đ¯ Validation Overview
Pre-push hooks perform comprehensive validation:
Code Quality Checks
- ESLint validation with project-specific rules
- TypeScript compilation verification
- Prettier formatting validation
- Import/export consistency checks
Testing Requirements
- Unit test execution for changed files
- Integration test validation
- Test coverage threshold enforcement
- Snapshot test verification
Security Validation
- Dependency vulnerability scanning
- Sensitive data detection
- License compliance verification
- Security rule enforcement
đ Quick Setup
Using npm scripts (Recommended):
# Install dependencies
npm install --save-dev husky lint-staged
# Initialize husky
npx husky install
# Add pre-push hook
npx husky add .husky/pre-push "npm run pre-push"
Add to your package.json
:
{
"scripts": {
"pre-push": "lint-staged && npm run test:changed"
},
"lint-staged": {
"*.{js,jsx,ts,tsx}": ["eslint --fix", "prettier --write"],
"*.{json,md,yml,yaml}": ["prettier --write"]
}
}
Manual Git hook setup:
# Create pre-push hook
cat > .git/hooks/pre-push << 'EOF'
#!/bin/sh
npm run lint && npm run test && npm run build
EOF
# Make executable
chmod +x .git/hooks/pre-push
Husky configuration:
// .huskyrc.json
{
"hooks": {
"pre-push": "npm run pre-push"
}
}
đ§ Advanced Configuration
Conditional Validation
Skip Options
Environment-based skipping:
# Skip validation in development
if [ "$NODE_ENV" = "development" ]; then
echo "đ¨ Emergency commit detected - skipping validation"
exit 0
fi
# Skip for specific commit messages
if git log -1 --pretty=%B | grep -q "\[skip-validation\]"; then
echo "âī¸ Validation skipped by commit message"
exit 0
fi
File-based conditional checks:
# Only run tests if test files changed
if git diff --cached --name-only | grep -q "\.test\.\|\.spec\."; then
npm run test:changed
fi
Branch-specific validation:
BRANCH=$(git rev-parse --abbrev-ref HEAD)
case $BRANCH in
main|master) npm run lint && npm run test && npm run build ;;
develop) npm run lint && npm run test:quick ;;
critical) npm run lint && npm run test:critical ;;
esac
đ¨ Troubleshooting
Common Issues
â ī¸ Warning: Hook Bypassing: Developers can bypass hooks with
git push --no-verify
. Monitor for this in CI.
Hook Not Running:
# Check if hook is executable
ls -la .git/hooks/pre-push
# Make executable if needed
chmod +x .git/hooks/pre-push
Performance Issues:
# Run only changed files
npm run lint -- --cache
npm run test -- --changedSince=HEAD~1
Memory Issues:
# Increase Node.js memory limit
export NODE_OPTIONS="--max-old-space-size=4096"
đ Next Steps
- CI/CD Setup â - Continuous integration configuration
- QA Automation â - Quality assurance and testing systems
- Contributing â - Development workflow and guidelines
â Success: Quality Gate: Pre-push hooks serve as the first line of defense, ensuring only high-quality code reaches the repository and reducing CI failures.