📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Git & GitHub Git Hooks

Git Hooks

5 min read
Run linting and tests automatically with pre-commit hooks and validate commit messages.

Git Hooks

# Local hooks in .git/hooks/
# pre-commit  — run before commit is created
# commit-msg  — validate commit message
# pre-push    — run before git push
# post-merge  — after merge completes

# .git/hooks/pre-commit (make executable)
#!/bin/sh
npm run lint
npm test
# Exit non-zero to abort commit

# .git/hooks/commit-msg
#!/bin/sh
MSG=$(cat "$1")
if ! echo "$MSG" | grep -qE "^(feat|fix|docs|style|refactor|test|chore):"; then
    echo "Error: commit message must start with type: feat|fix|docs|..."
    exit 1
fi

# Share hooks with team using Husky (Node.js)
npx husky init
echo "npm test" > .husky/pre-commit