3 Hours

13. Git Hooks: Automating Workflows

Automation

Git hooks are scripts that run automatically on Git events. They enable automation: run tests before commits, enforce commit message conventions, and format code automatically. Git hooks are essential for enforcing quality and consistency. They're how professional teams maintain code quality.

The key insight is that git hooks are local automation. They run on your machine, not on a server. This makes them powerful for enforcing local quality before pushing. Hooks are stored in the .git/hooks directory. This module covers the most useful hooks and how to implement them.

Git hooks are the first line of defense against bad code. They catch issues before they're committed, preventing them from ever reaching the repository. This is much cheaper and faster than catching issues in CI.

Pre-commit Hooks

Pre-commit hooks run before a commit is created. They're used to validate code, run tests, format code, and enforce commit message conventions. If a pre-commit hook fails, the commit is aborted. This prevents bad code from entering the repository. Common pre-commit hooks: lint code, run tests, format code, check for secrets, and enforce commit message format. Pre-commit hooks are the most important hooks for quality enforcement.

Pre-commit hooks are your first line of defense. They run fast, so they don't slow down development. But they catch issues early. Linting, formatting, and secret checking are common pre-commit hooks. These hooks ensure that only quality code is committed.

Commit-msg Hooks

Commit-msg hooks run after the commit message is entered but before the commit is created. They validate the commit message format. This enforces consistency and makes the commit history more readable. Common commit-msg hooks: enforce conventional commits format, check for issue references, and enforce a minimum length. Consistent commit messages improve the maintainability of the repository.

Commit message consistency is a mark of a professional team. Conventional commits format makes it easy to understand the purpose of each commit. It also enables automated changelog generation. Commit-msg hooks enforce this consistency automatically.

Pre-push Hooks

Pre-push hooks run before a push to a remote. They're used for more expensive checks that don't need to run on every commit. Common uses: run the full test suite, build the project, or run integration tests. Pre-push hooks catch issues that might have been missed by pre-commit hooks. They're a safety net before pushing to the remote repository.

Pre-push hooks are your final check before sharing code. They run the full test suite, which might take a few minutes. This ensures that you're not pushing broken code. Pre-push hooks prevent embarrassing failures in CI.

Step-by-Step: Setting Up Git Hooks

Follow these steps to set up your own Git hooks and automate quality checks.

Step 1: Navigate to the hooks directory
cd .git/hooks
You'll see sample hook files with .sample extensions. These are examples you can enable.

Step 2: Create a pre-commit hook
Create a file called pre-commit with this content:
#!/bin/sh<br><br># Run linter<br>echo <strong><code>Running ESLint...
npx eslint . || exit 1

# Run tests
echo Running tests...
npm test || exit 1
Make it executable: chmod +x pre-commit

Step 3: Create a commit-msg hook
Create a file called commit-msg:
#!/bin/sh<br><br># Enforce conventional commits format<br>COMMIT_MSG=$(cat $1)<br>if ! echo <strong><code>$COMMIT_MSG | grep -qE '^(feat|fix|docs|style|refactor|perf|test|chore)(\(.+\))?: .+'; then
echo "Error: Commit message must follow conventional commits format"
exit 1
fi
Make it executable: chmod +x commit-msg

Step 4: Create a pre-push hook
Create a file called pre-push:
#!/bin/sh<br><br># Run full test suite before pushing<br>echo <strong><code>Running full test suite...
npm run test:integration || exit 1
Make it executable: chmod +x pre-push

Step 5: Test your hooks
Make a change and try to commit with a non-conventional message — it should be rejected. Try to commit with linting errors — it should fail. Try to push with failing tests — it should be blocked.

Try It Yourself: Add a hook that checks for secrets. Add a hook that formats code automatically. Share hooks with your team by storing them in a hooks directory and symlinking.

Knowledge Check

Ready to test your understanding of 13. Git Hooks: Automating Workflows?

Back