git-hooks
This page shows how to catch two common mistakes before they reach the remote: a commit message that breaks the Conventional Commits format, and a push that does not typecheck. The checks run on your machine, so you find out in seconds rather than after CI.
What you get
Two Git hooks managed by lefthook: commitlint on every commit message, and a typecheck before every push. This feature is off by default.
Add it
What changed at the project root:
The prepare script runs lefthook install, so hooks are set up after every pnpm install. Root dev dependencies added:
Use it
Run pnpm install once so the prepare script installs the hooks. After that you work as usual. A message that does not follow the format is rejected at commit time:
The commit is refused with commitlint's list of problems. This one passes:
Before git push, the hook runs pnpm typecheck and stops the push if it fails.
Configure it
The feature has no install options. Both files are small and yours to edit:
How it works
lefthook writes small scripts into your repository's hooks folder. Those scripts call back into lefthook, which reads lefthook.yml.
The prepare script re-runs the install, so a fresh clone gets the hooks after pnpm install. commitlint receives the path to the message file as {1} and checks it against the conventional config.
Gotchas
-
A commit can skip hooks with the
--no-verifyflag. Treat hooks as a convenience and keep CI as the check that cannot be skipped. -
lefthook is allowed to run its install script in the generated
pnpm-workspace.yaml. If you edit that file, keep the entry orpreparehas nothing to run. -
Typecheck on push can take a while in a large project. Move it out of
lefthook.ymlif it slows you down and rely on CI.
Reference
- commit-prompt, which builds messages that pass this check
- ci-github
- lefthook
- commitlint