Par Anonyme (non vérifié) , 29 avril 2026

Ce que vous accomplirez

Vous configurerez des hooks pre-commit et pre-push qui linkent, formatent, type-checkent et testent votre code.

Choisir un framework

  • Husky + lint-staged pour projets Node.
  • framework pre-commit pour Python ou polyglottes.
  • core.hooksPath pour scripts shell custom.

Chemin 1 : Husky pour Node

npm install --save-dev husky lint-staged eslint prettier
npx husky init
# .husky/pre-commit
#!/usr/bin/env sh
. "$(dirname "$0")/_/husky.sh"
npx lint-staged

# package.json
"lint-staged": {
  "*.{js,ts,tsx,jsx}": [
    "eslint --fix",
    "prettier --write"
  ],
  "*.{json,md,yml,yaml}": ["prettier --write"]
}
# .husky/pre-push
#!/usr/bin/env sh
. "$(dirname "$0")/_/husky.sh"
npm run typecheck
npm test

Chemin 2 : framework pre-commit

pip install pre-commit
# .pre-commit-config.yaml
repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.6.0
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer
      - id: check-yaml
      - id: check-merge-conflict
  - repo: https://github.com/astral-sh/ruff-pre-commit
    rev: v0.5.0
    hooks:
      - id: ruff
        args: [--fix]
      - id: ruff-format
  - repo: https://github.com/gitleaks/gitleaks
    rev: v8.18.0
    hooks:
      - id: gitleaks
pre-commit install
pre-commit install --hook-type pre-push
pre-commit run --all-files

Chemin 3 : hooks shell partagés

mkdir .githooks
cat > .githooks/pre-commit <<'EOF'
#!/usr/bin/env bash
set -e
files=$(git diff --cached --name-only --diff-filter=ACMR | grep -E '\\.(js|ts)$' || true)
[ -z "$files" ] && exit 0
echo "$files" | xargs npx eslint --max-warnings=0
echo "$files" | xargs npx prettier --check
EOF
chmod +x .githooks/pre-commit

git config core.hooksPath .githooks

Bonnes pratiques

  • Lancer seulement sur les fichiers changés.
  • Visez <5 secondes pre-commit, <30 secondes pre-push.
  • Hooks fixables.
  • Reflet en CI.

Linting des messages de commit

npm install --save-dev @commitlint/cli @commitlint/config-conventional

# commitlint.config.js
module.exports = { extends: ['@commitlint/config-conventional'] };

# .husky/commit-msg
#!/usr/bin/env sh
. "$(dirname "$0")/_/husky.sh"
npx --no -- commitlint --edit $1

Contourner

git commit --no-verify
git push --no-verify