πŸ‹
Menu
Best Practice Beginner 1 min read 253 words

Git Hooks for Automated Code Quality

Git hooks run scripts automatically before or after Git events. Set up pre-commit hooks for linting, formatting, and testing to catch issues before they reach your repository.

Why Git Hooks?

Git hooks provide automatic quality gates in your development workflow. A pre-commit hook can run linters, formatters, and type checkers on staged files, catching issues before they're committed. A pre-push hook can run the full test suite before code leaves your machine.

Essential Pre-Commit Hooks

Lint staged files (not the entire project) for speed. Format code automatically β€” if the formatter changes a file, stage the changes and allow the commit. Check for debug statements, TODO comments, and hardcoded secrets. Validate file sizes to prevent accidental binary commits. Run type checking on changed files.

Setting Up with pre-commit Framework

The pre-commit framework (pre-commit.com) manages hook installation and execution. Define hooks in .pre-commit-config.yaml, and the framework handles virtual environments, caching, and updates. Hooks run only on staged files, keeping execution fast even in large repositories.

Commit Message Hooks

The commit-msg hook validates commit message format. Enforce conventional commit prefixes (feat:, fix:, docs:), minimum length, and ticket reference patterns. This enables automatic changelog generation and semantic versioning.

Team Adoption

Install hooks automatically when developers clone the repository. Add a setup script that runs pre-commit install or configure your package manager to include this step. Document which hooks are active and how to bypass them in emergencies (git commit --no-verify for rare exceptions).

Performance Considerations

Keep pre-commit hooks under 5 seconds for developer experience. Run expensive checks (full test suite, integration tests) in pre-push hooks or CI/CD instead. Cache results where possible β€” don't re-lint unchanged files.

κ΄€λ ¨ 도ꡬ

κ΄€λ ¨ 포맷

κ΄€λ ¨ κ°€μ΄λ“œ