Skip to content

Git Hooks

Git

Automate workflows with Git hooks: pre-commit, pre-push, commit-msg.

Official Site
01

Overview

Hooks are scripts in .git/hooks/ that run on events. pre-commit: validate before commit. pre-push: run tests before push. commit-msg: validate message format. Use husky/pre-commit framework for shared hooks in repo.

bash
# .git/hooks/pre-commit (sample)
#!/bin/sh
npm run lint && npm run test
# With husky
npx husky add .husky/pre-commit "npm run lint"
# pre-push hook
npx husky add .husky/pre-push "npm run test"
# commit-msg (conventional commits)
npx husky add .husky/commit-msg "npx commitlint --edit $1
02

Best Practices

Use husky for shared hooks (committed to repo). pre-commit for lint/format. pre-push for tests. commit-msg for conventional commits. Keep hooks fast. Use commitlint for message format. Use lint-staged for speed.)

Ready to try Git Hooks?

Visit the official site for the latest version and full documentation.

Visit Git Hooks