Von Gast (nicht überprüft) , 29 April 2026

Was Sie erreichen werden

Sie werden Pre-Commit- und Pre-Push-Hooks konfigurieren, die Code linten, formatieren, type-checken und testen.

Ein Framework wahlen

  • Husky + lint-staged fur Node-Projekte.
  • pre-commit Framework fur Python.
  • core.hooksPath fur custom Shell-Skripte.

Pfad 1: Husky fur 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

Pfad 2: pre-commit Framework

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

Pfad 3: geteilte Shell-Hooks

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

Best Practices

  • Nur auf geanderten Dateien ausfuhren.
  • Auf <5 Sekunden Pre-Commit zielen.
  • Hooks fixbar machen.
  • In CI spiegeln.

Mehr Checks hinzufugen

  • Type-Check.
  • Geheimnis-Erkennung.
  • Commit-Message-Format.
  • Branch-Name-Format.

Commit-Message-Linting

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