Commit-msg Hook

Commit-msg Hook

A commit-msg hook runs after you write the commit message but before the commit is finalized. It receives the path to a temporary file containing the message as its first argument ($1). If the script exits non-zero, the commit is aborted.

Create the hook

$ cat > .git/hooks/commit-msg << 'EOF'
#!/bin/sh
# Enforce minimum message length
if [ $(wc -c < "$1") -lt 10 ]; then
  echo "Error: commit message too short"
  exit 1
fi
EOF
$ chmod +x .git/hooks/commit-msg

Common validations

Example: enforce Conventional Commits

$ cat > .git/hooks/commit-msg << 'EOF'
#!/bin/sh
pattern="^(feat|fix|docs|chore|refactor|test|ci|style)(\(.+\))?: .{3,}"
if ! grep -qE "$pattern" "$1"; then
  echo "Error: message must follow Conventional Commits format"
  exit 1
fi
EOF
$ chmod +x .git/hooks/commit-msg

Sharing and bypassing

Same as other hooks — see the Hooks recipe for core.hooksPath and --no-verify.

On this page