Git Hooks: Workflow automatisieren | Enjyn Gruppe
Hallo Welt
Hallo Welt
Original Lingva Deutsch
Übersetzung wird vorbereitet...
Dieser Vorgang kann bis zu 60 Sekunden dauern.
Diese Seite wird erstmalig übersetzt und dann für alle Besucher gespeichert.
0%
DE Zurück zu Deutsch
Übersetzung durch Lingva Translate

235 Dokumentationen verfügbar

Wissensdatenbank

Git Hooks Automatisierung

Zuletzt aktualisiert: 20.01.2026 um 10:03 Uhr

Git Hooks: Workflow automatisieren

Git Hooks führen automatisch Scripts bei Git-Aktionen aus. Perfekt für Linting, Tests und Code-Qualität.

Verfügbare Hooks

Hook Wann Verwendung
pre-commit Vor Commit Linting, Formatting
prepare-commit-msg Commit-Message vorbereiten Template einfügen
commit-msg Nach Message-Eingabe Message validieren
pre-push Vor Push Tests ausführen
post-merge Nach Merge Dependencies installieren

Hook erstellen (manuell)

# Hooks liegen in .git/hooks/
cd .git/hooks

# Beispiel: pre-commit
cat > pre-commit << 'EOF'
#!/bin/sh
echo "Running pre-commit checks..."

# Linting
npm run lint
if [ $? -ne 0 ]; then
    echo "Lint errors found. Commit aborted."
    exit 1
fi

# Tests
npm test
if [ $? -ne 0 ]; then
    echo "Tests failed. Commit aborted."
    exit 1
fi

echo "All checks passed!"
exit 0
EOF

# Ausführbar machen
chmod +x pre-commit

Husky (empfohlen für Node.js)

# Installation
npm install husky --save-dev
npx husky init

# Pre-commit Hook
echo "npm run lint" > .husky/pre-commit

# Pre-push Hook
echo "npm test" > .husky/pre-push

# Commit-Message Validierung
npm install @commitlint/cli @commitlint/config-conventional --save-dev

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

# Hook erstellen
echo "npx commitlint --edit \$1" > .husky/commit-msg

lint-staged

# Nur geänderte Dateien linten
npm install lint-staged --save-dev

# package.json
{
  "lint-staged": {
    "*.{js,ts}": ["eslint --fix", "prettier --write"],
    "*.css": ["stylelint --fix"],
    "*.{json,md}": ["prettier --write"]
  }
}

# .husky/pre-commit
npx lint-staged

Pre-push: Tests ausführen

#!/bin/sh
# .husky/pre-push

echo "Running tests before push..."

# Nur wenn main/master
branch=$(git rev-parse --abbrev-ref HEAD)
if [ "$branch" = "main" ] || [ "$branch" = "master" ]; then
    npm test
    if [ $? -ne 0 ]; then
        echo "Tests failed! Push aborted."
        exit 1
    fi
fi

exit 0

Commit-Message Format

# Conventional Commits
# type(scope): description

# Types:
# feat:     Neue Feature
# fix:      Bug Fix
# docs:     Dokumentation
# style:    Formatting (kein Code-Change)
# refactor: Code-Änderung ohne Feature/Fix
# test:     Tests hinzufügen
# chore:    Build, Dependencies, etc.

# Beispiele:
feat(auth): add password reset functionality
fix(api): handle null response from server
docs(readme): update installation instructions
refactor(user): extract validation logic

# commit-msg Hook zur Validierung
#!/bin/sh
# .husky/commit-msg

commit_regex='^(feat|fix|docs|style|refactor|test|chore)(\(.+\))?: .{1,72}$'
commit_msg=$(cat "$1")

if ! echo "$commit_msg" | grep -Eq "$commit_regex"; then
    echo "Invalid commit message format!"
    echo "Expected: type(scope): description"
    echo "Example: feat(auth): add login feature"
    exit 1
fi

Hooks mit Team teilen

# Problem: .git/hooks ist nicht im Repository

# Lösung 1: Husky (empfohlen)
# Hooks in .husky/ werden automatisch installiert

# Lösung 2: Custom Scripts Directory
# scripts/hooks/ im Repo, dann:
git config core.hooksPath scripts/hooks

# Lösung 3: Symlinks (Makefile)
# Makefile
setup:
    ln -sf ../../scripts/hooks/pre-commit .git/hooks/pre-commit
    ln -sf ../../scripts/hooks/commit-msg .git/hooks/commit-msg

PHP: Pre-commit mit Composer

{
    "scripts": {
        "lint": "php-cs-fixer fix --dry-run --diff",
        "test": "phpunit",
        "pre-commit": [
            "@lint",
            "@test"
        ]
    }
}

# .git/hooks/pre-commit
#!/bin/sh
composer run pre-commit

Python: pre-commit Framework

pip install pre-commit

# .pre-commit-config.yaml
repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.5.0
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer
      - id: check-yaml

  - repo: https://github.com/psf/black
    rev: 24.1.0
    hooks:
      - id: black

  - repo: https://github.com/PyCQA/flake8
    rev: 7.0.0
    hooks:
      - id: flake8

# Installieren
pre-commit install

# Manuell ausführen
pre-commit run --all-files

Hook umgehen (Notfall)

# Wenn nötig (selten verwenden!)
git commit --no-verify -m "Emergency fix"
git push --no-verify
💡 Tipp: Halten Sie Pre-commit Hooks schnell (< 10 Sekunden). Langsame Checks gehören in CI/CD oder Pre-push.

Weitere Informationen

Enjix Beta

Enjyn AI Agent

Hallo 👋 Ich bin Enjix — wie kann ich dir helfen?
120