name: git-hooks description: Set up git hooks with lefthook — pre-commit and pre-push automation, parallel hook execution, linting, formatting, test gating. license: MIT
Git Hooks with Lefthook
Lefthook is a fast, powerful, and simple Git hooks manager written in Go. It allows you to run scripts/commands automatically when git events occur (commit, push, merge, etc.).
Installation
Lefthook can be installed via multiple methods. Choose the one that fits your ecosystem:
JavaScript/Node.js (Recommended)
npm install --save-dev lefthook
# or: yarn add --dev lefthook
# or: pnpm add -D lefthook
Ruby
gem install lefthook
# or add to Gemfile: gem "lefthook"
Go
go install github.com/evilmartians/lefthook/v2@v2.1.3
Python
pip install lefthook
# or: uv add --dev lefthook
System Package Managers
| OS | Command |
|---|---|
| macOS/Linux | brew install lefthook |
| Windows | winget install evilmartians.lefthook |
| Arch Linux | yay -S lefthook |
| Ubuntu/Debian | apt install lefthook (via Cloudsmith) |
Verify Installation
lefthook --version
lefthook check-install
Quick Start
# 1. Create configuration
cat > lefthook.yml << 'EOF'
pre-commit:
parallel: true
jobs:
- run: yarn lint {staged_files}
glob: "*.{js,ts,jsx,tsx}"
- run: yarn test
EOF
# 2. Install Git hooks
lefthook install
# 3. Test it
git commit -m "test"
Configuration File
Supported File Names and Formats
YAML (recommended):
lefthook.yml,lefthook.yaml.lefthook.yml,.lefthook.yaml.config/lefthook.yml
Other formats:
- TOML:
lefthook.toml - JSON:
lefthook.json - JSONC:
lefthook.jsonc
Local Overrides
Create lefthook-local.yml for personal settings (add to .gitignore):
# lefthook-local.yml
pre-commit:
commands:
test:
skip: true # Skip tests locally
CLI Commands
install
Installs Git hooks to .git/hooks/:
lefthook install # Install all hooks
lefthook install pre-commit # Install specific hook
lefthook install -f # Force overwrite
uninstall
Removes installed hooks:
lefthook uninstall
run
Manually run a hook:
lefthook run pre-commit
lefthook run my-custom-hook
lefthook run pre-commit --all-files # Use all files, not staged
lefthook run pre-commit --force # Run even with no changes
lefthook run pre-commit --job lint # Run specific job
lefthook run pre-commit --tag security # Run by tag
add
Create new hook with script template:
lefthook add pre-push --dirs
# Then edit .lefthook/pre-push/audit.sh
# And add to lefthook.yml
validate
Check configuration for errors:
lefthook validate
dump
Show merged configuration:
lefthook dump
check-install
Verify hooks are installed:
lefthook check-install
# Exit code 0 = installed, 1 = not installed
self-update
Update lefthook binary:
lefthook self-update
Environment Variables
| Variable | Purpose | Example |
|---|---|---|
LEFTHOOK=0 |
Disable lefthook | LEFTHOOK=0 git commit |
LEFTHOOK_VERBOSE=1 |
Debug output | LEFTHOOK_VERBOSE=1 lefthook run pre-commit |
LEFTHOOK_CONFIG |
Custom config | LEFTHOOK_CONFIG=~/config.yml lefthook run pre-commit |
LEFTHOOK_EXCLUDE |
Skip tags | LEFTHOOK_EXCLUDE=ruby lefthook run pre-commit |
CI=true |
CI mode (skip install) | CI=true npm install |
NO_COLOR=1 |
Disable colors | NO_COLOR=1 lefthook run pre-commit |
Hook Configuration
Available Hooks
pre-commit- Before commitcommit-msg- Validate commit messagepre-push- Before pushpre-rebase- Before rebasepost-merge- After mergepost-checkout- After checkout- Custom hooks - Any name works
Execution Options
pre-commit:
parallel: true # Run jobs concurrently (default: false)
piped: true # Stop on first failure (default: false)
setup: # Commands to run before jobs
- run: bundle install
jobs: # Define tasks
- run: yarn lint
File Variables
| Variable | Description |
|---|---|
{staged_files} |
Files staged for commit (pre-commit) |
{push_files} |
Files being pushed (pre-push) |
{all_files} |
All git-tracked files |
{files} |
Custom file list |
{cmd} |
Original command (for wrapping) |
{0}, {1}, ... |
Git hook arguments |
Job/Command Options
jobs:
- name: eslint # Job name (for merging)
run: yarn eslint # Command to execute
# File filtering
glob: "*.{js,ts}"
exclude: "*.config.js"
file_types: [ruby, javascript]
files: git diff --name-only master
# Execution
root: frontend/ # Working directory
env: # Environment variables
NODE_ENV: test
priority: 1 # Run order (lower = first)
# Behavior
stage_fixed: true # Auto-add fixed files (pre-commit only)
interactive: true # Run with stdin
skip: true # Skip this job
tags: [security, lint] # Tag for filtering
# Custom output
fail_text: "Fix lint errors!"
Practical Examples
ESLint + Prettier + TypeScript
pre-commit:
parallel: true
jobs:
- name: eslint
glob: "*.{js,ts,jsx,tsx}"
run: yarn eslint {staged_files} --fix
stage_fixed: true
- name: prettier
glob: "*.{js,ts,jsx,tsx,json,css}"
run: yarn prettier --write {staged_files}
stage_fixed: true
- name: types
run: yarn tsc --noEmit
Ruby with RuboCop + RSpec
pre-commit:
parallel: true
jobs:
- name: rubocop
glob: "*.rb"
exclude:
- config/routes.rb
- config/application.rb
run: bundle exec rubocop --force-exclusion -- {staged_files}
stage_fixed: true
- name: tests
run: bundle exec rspec --format progress
pre-push:
piped: true
jobs:
- run: bundle exec rails db:test:prepare
- run: bundle exec rspec --format progress
Monorepo
pre-commit:
parallel: true
jobs:
- name: web-lint
glob: "packages/web/**/*.{js,ts,jsx,tsx}"
run: cd packages/web && yarn lint {staged_files}
- name: api-lint
glob: "packages/api/**/*.go"
run: cd packages/api && go vet ./...
- name: shared-lint
glob: "packages/shared/**/*.{ts,tsx}"
run: cd packages/shared && yarn lint
Docker-based Development
# lefthook.yml
templates:
dip: docker-compose run --rm app
pre-commit:
jobs:
- run: {dip} yarn lint
- run: {dip} yarn test
# lefthook-local.yml (personal override)
pre-commit:
jobs:
- name: lint
run: {dip} yarn lint # wraps with docker
Conditional Execution
pre-commit:
only:
- ref: main # Only on main branch
- ref: develop # Or develop
jobs:
- run: yarn lint
pre-push:
jobs:
- name: tests
skip:
- run: test "$SKIP_TESTS" = "1"
run: yarn test
Remote Configs (Team-wide)
# lefthook.yml
remotes:
company:
git_url: git@github.com:company/lefthook-config.git
ref: main
configs:
- lefthook.yml
Auto-Stage Fixed Files
pre-commit:
fail_on_changes: ci # Fail in CI only, not locally
jobs:
- run: yarn eslint --fix {staged_files}
glob: "*.{js,ts}"
stage_fixed: true # Automatically git add fixed files
- run: yarn stylelint --fix {staged_files}
glob: "*.css"
stage_fixed: true
Commit Message Validation
commit-msg:
jobs:
- name: conventional
run: npx commitlint --edit {1}
Fail on Changes
pre-commit:
fail_on_changes: always # never/always/ci/non-ci
fail_on_changes_diff: true # Show diff on failure
jobs:
- run: yarn lint
Best Practices
- Keep hooks fast - Use parallel execution, skip slow checks on small commits
- Use staged files - Only lint what's changed, not everything
- Fail fast - Put fastest checks first, use
piped: truefor dependent commands - Use stage_fixed - Let linters auto-fix and re-stage
- Local overrides - Use
lefthook-local.ymlfor personal settings - Tag filtering - Use
--tagto run subsets of jobs - Validate in CI - Use
lefthook run pre-commit --all-filesfor full checks - Custom tasks - Define
fixer:orlint-all:hooks for manual runs