Advanced · 5 min read
Git hooks: automate your checks
Run the linter before each commit, check the message format, block pushes to main.
What a hook is
An executable script Git runs at a given moment: pre-commit, commit-msg, pre-push… If it exits with an error, the action is cancelled.
Share them with the team
.git/hooks is not versioned. Put the scripts in a versioned folder and point Git to it (tools like Husky or Lefthook do this for you).
$ mkdir .githooks$ git config core.hooksPath .githooks
Example: no commit on main
A pre-commit hook that refuses commits made directly on main. Make the file executable (chmod +x).
#!/bin/sh$ [ "$(git branch --show-current)" = "main" ] && echo "No commit on main" && exit 1$ exit 0
Skipping a hook
--no-verify skips pre-commit and commit-msg. Hooks are a safety net, not a security measure: protect branches on the server too.