liveBook · Manning

Git hook

As explained in Automating API Delivery

A Git hook is a customizable script that runs automatically in response to an event in a Git repository.

A Git hook is an executable script that runs automatically when an event occurs in a repository. Client-side hooks can reject invalid commits before creation; server-side hooks can validate pushed commits. This fail-fast control supports GitOps and APIOps when version control is the project’s source of truth. section 8.8

Implement hooks as lightweight governance controls:

  • Use a pre-commit hook to require a commit message containing a JIRA issue reference such as DEV-1234. section 8.8
  • Keep hooks fast, reliable, documented, and non-mutating unless the committer explicitly consents. section 8.8
  • Distribute client-side hooks deliberately because cloning a repository does not install them; a hook package manager can manage installation and execution. section 8.8
#!/bin/bash

commit_msg_file=$1

# Read the commit message file
commit_msg=$(cat "$commit_msg_file")

# Pattern to match Jira issue reference (DEV-XXXX)
Pattern="DEV-[0-9]{4}"

# Check if the commit message contains the Jira issue reference
if [[ ! $commit_msg =~ $pattern ]]; then
    echo "Error: Commit message does not contain a valid 
 Jira issue reference in the format DEV-XXXX."
    echo "Please include a Jira issue reference in your commit message."
    exit 1
fi

Use the Bash example as a template for reading the commit-message file, matching DEV-XXXX, and rejecting nonconforming messages. section 8.8

How Automating API Delivery covers it

How deep it goes
Explains client/server timing, implementation practices, installation limits, and a Bash validation pattern without covering complete deployment automation.
Examples
Demonstrates a pre-commit Bash check requiring a DEV-XXXX JIRA reference in the commit message.
What it assumes
Assumes a Git repository and executable-script setup while defining hooks and their governance role.
What you can do after
Operate/configure a Git hook that validates commit messages and understand its workstation-distribution and server-side enforcement trade-offs.
How it connects
Connects Git hooks to commit policies, GitOps, APIOps, pull requests, and hook package managers.

Also explained in

Compare how 2 books explain git hook →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Automating API Delivery. The section links point to the parts of the book it draws on.