What are hooks in Git? Can you give examples?
Git hooks are scripts that run automatically when specific Git events occur (like committing
or pushing).
They live inside .git/hooks/.
Common examples:
Follow:
pre-commit: Run linting or unit tests before a commit
# .git/hooks/pre-commit
npm run lint || exit 1
pre-push: Prevent pushes to main
if [ "$(git rev-parse --abbrev-ref HEAD)" == "main" ]; then
echo "You can't push directly to main!"
exit 1
Example:
In a team, we set a pre-commit hook to check code formatting with ESLint before any
commit — ensuring consistency across all contributors.