---
title: "Git Hooks: Automation in the Development Workflow"
lang: en
type: knowledge
canonical: https://www.arocom.de/en/knowledge/development/git-hooks
alternate: https://www.arocom.de/wissen/entwicklung/git-hooks
author: "Axel Roth"
last_updated: 2025-03-15
---

# Git Hooks: Automation in the Development Workflow

Git Hooks are scripts that run automatically during Git actions, before
a commit, after a merge or before a push. They enforce coding standards,
start tests and prevent faulty commits. Since 2012, arocom has used Git
Hooks in Drupal projects as the first quality assurance step in the
development workflow.

## Git Hooks: Automation in the Development Workflow

Code reviews and CI/CD pipelines catch errors, but late in the
process. Git Hooks act earlier: directly on the developer's
machine, before the code reaches the repository.

## What Git Hooks Are

Git Hooks are shell scripts in the .git/hooks/ directory of a
repository. Git executes them during specific events. The most
important:

**pre-commit.** Runs before every commit. Checks coding
standards, syntax errors and formats code automatically. If the
script fails, the commit is aborted.

**commit-msg.** Checks the commit message. Enforces naming
conventions like "PROJ-123: description" for traceability.

**pre-push.** Runs before a push. Starts unit tests or
linting, the push is aborted if tests fail.

**post-merge.** Runs after a merge. Automatically executes
"composer install" or "drush updatedb" so the local
environment stays up to date.

![Git hook lifecycle, pre-commit, commit-msg, pre-push](https://www.arocom.de/images/screenshots/wissen/entwicklung/git-hooks.webp)

*Locally enforced Git hooks block faulty commits before they reach the remote.*

## Git Hooks for Drupal Projects

**PHP CodeSniffer.** In the pre-commit hook, PHPCS checks code
against Drupal coding standards. Incorrectly formatted code
is not committed.

**PHPStan.** Static analysis finds type errors and undefined
variables before code is executed.

**YAML validation.** Drupal configurations are stored as YAML
files. A hook checks syntax and prevents faulty
configurations.

**Automatic formatting.** Tools like PHP-CS-Fixer format code
automatically in the pre-commit hook. No manual cleanup
needed.

`.git/hooks/pre-commit`

```bash
#!/usr/bin/env bash
set -euo pipefail

STAGED_PHP=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(php|module|inc|install|theme)$' || true)
[ -z "$STAGED_PHP" ] && exit 0

echo "→ PHPCS (Drupal/DrupalPractice)"
vendor/bin/phpcs --standard=Drupal,DrupalPractice $STAGED_PHP

echo "→ PHPStan (level 1)"
vendor/bin/phpstan analyse --no-progress --level=1 $STAGED_PHP

echo "→ YAML lint (config/sync)"
git diff --cached --name-only --diff-filter=ACM | grep '\.yml$' \
  | xargs -r vendor/bin/yaml-lint
```

*Example, pre-commit hook for a typical Drupal project with PHPCS and PHPStan.*

## Sharing Hooks Across the Team

Git Hooks are local and not automatically shared. Tools like
Husky, GrumPHP or Captain Hook solve this: the hook
configuration is versioned in the repository and automatically
installed during "composer install". Every team member works
with the same quality checks.

## Optimize Your Development Workflow?

arocom sets up Git Hooks and CI/CD pipelines for Drupal
projects. The [Future Check](https://www.arocom.de/en/future-check) evaluates your
development process. Starting at EUR 2,500 plus VAT, credited
toward the follow-up project.

**From arocom projects:**

- Hopt + Schuler needs job openings on the careers page in real time, sourced from the JobCluster system. We delivered this via a custom module against the JobCluster API, vacancies appear without manual upkeep, the recruiting workflow runs centrally in JobCluster, the site reads live.
- Drupal as a dashboard platform? For marego it was exactly the right choice: editable data structures, granular roles, integrated Excel/PDF pipelines. The tool now consolidates reporting that used to live in five different tools.

### Can Git Hooks be bypassed?

Yes, with the --no-verify option. This is intentionally possible for exceptional cases. The CI/CD pipeline then catches issues as a second line of defense. Git Hooks are the first, not the only quality assurance.

### Do Git Hooks slow down the workflow?

Well-configured hooks run in seconds. The pre-commit hook should only check changed files, not the entire repository. Slow tests belong in the pre-push hook or the CI/CD pipeline.

### Which tools does arocom recommend for Git Hooks?

GrumPHP for PHP projects and Drupal. It integrates PHPCS, PHPStan, YAML lint and other checkers in one configurable pre-commit hook. The configuration is distributed via Composer.

## Read more

- [Git in the Drupal Context](https://www.arocom.de/en/knowledge/development/git): Version control explained
- [DevOps](https://www.arocom.de/en/knowledge/development/devops): Automation in development
- [Deployment](https://www.arocom.de/en/knowledge/development/deployment): Delivering code safely
