Video in progress
What are Git hooks? Why is enforcing commit rules in a team with client-side hooks alone not enough?
A hook is a script that Git runs automatically at certain moments (commit, push, merge). Client-side hooks live inside .git/hooks, are not distributed with a clone, and can be bypassed with --no-verify. So the main check has to be in CI and on the server side.
📖 Answer
Commonly used client-side hooks:
pre-commit: formatting, a linter, a secret scanner;commit-msg: checking the message format (for example, whether there is a task number);pre-push: quick unit tests.
#!/bin/sh
# .githooks/commit-msg
if ! grep -qE '^(feat|fix|refactor|docs|test|chore)(\(.+\))?: ' "$1"; then
echo "Commit message Conventional Commits formatida bo'lishi kerak"
exit 1
fi
The problem: the .git/hooks folder is not versioned. To distribute hooks to the team you put them in a folder in the repository and run git config core.hooksPath .githooks, or install them through a Maven/Gradle plugin or the pre-commit framework. Even then, someone does not install them, and someone does git commit --no-verify.
So I treat hooks as a convenience for the developer: they catch a mistake 10 minutes before CI would. The real rule lives elsewhere: the same checks in CI, required checks in branch protection, and on the server a pre-receive hook (on self-hosted GitLab/Gitea) or GitHub's push rules and secret scanning.
One more piece of advice: do not make the pre-commit hook slow. People will turn off a hook that makes them wait 40 seconds within a week.
Sign up to mark studied questions and save your progress.