Learn to enhance code quality with Husky git hooks, customized for workflow optimization and fostering excellence in development teams. The "pre-commit" hook for formatting code, the "commit-msg" hook to enforce clear commit messages, and the "pre-push" hook to verify deployable code.
Git hooks play a crucial role in maintaining code quality and preventing faults throughout the development process. One powerful tool for implementing these hooks is Husky, which enables developers to automate various quality checks before and after committing code. In this blog post, we'll explore how to use Husky to implement for essential git hooks: pre-commit
, commit-msg
, prepare-commit-msg
, and pre-push
to enhance your codebase quality and boost confidence in the shipping process.
The pre-commit
hook acts as the first line of defense, ensuring that every commit meets certain quality standards before it's added to the repository. In the .husky
folder, we've implemented a pre-commit
hook that leverages pretty-quick to format staged files automatically.
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
node_modules/.bin/pretty-quick --staged
This ensures that code formatting is consistent, contributing to a clean and maintainable codebase.
Once the pre-commit
hook succeeds, the commit message hook, powered by @commitlint/cli and @commitlint/config-conventional, checks the commit message itself against a predefined convention.
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
yarn commitlint --edit "$1"
Standardizing commit messages is crucial for clear communication within the development team and maintaining a meaningful version history. Here we have a basic example but for full assistance in establishing a commit message convention, refer to the Commit Message Convention blog.
The pre-push
hook runs before the git push
command, performing a series of checks to ensure the code is in a deployable state. This includes:
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
. "$(dirname -- "$0")/common.sh"
yarn test && yarn lint && yarn build && yarn docs
Running these checks before pushing code helps catch issues early, preventing faulty code from reaching the remote repository.
In many cases, you might find the prepare-commit-msg
hook unnecessary, but certain tools, like commitizen and @commitlint/prompt, rely on its functionality. We'll explore Commitizen in more detail in the Commit Message Convention blog.
While Husky provides a --no-verify
git flag for bypassing verification, it should be used sparingly and only in critical situations. Common scenarios include addressing a severe production bug or when you are confident that your changes won't impact the checked commands, such as when making documentation-only changes.
Husky is a flexible tool, allowing developers to tailor hooks to their specific needs. Consider integrating custom checks that align with your project's unique requirements. For instance, you might incorporate security scans, dependency audits, or other domain-specific validations.
By implementing Husky git hooks efficiently, developers can optimize their workflows and elevate the quality of their codebase. From enforcing formatting consistency to standardizing commit messages and running comprehensive tests and builds, Husky empowers teams to ship confidently and minimize the risk of faults. This will not only contribute in an improved development process but also foster a culture of excellence within your team.