Hooks

Git provides both server- and client-side hooks that allow custom actions to be taken on events. Generally, pre-* hooks can intercept operations by exiting with a non-zero status, and post-* hooks are well suited to sending notifications.

Configuration

Hooks can be relocated from the default .git/hooks directory with the core.hooksPath option, allowing them to be kept under version control.

Client-side hooks

  • prepare-commit-msg executes between the generation of the default commit message and the editor starting, allowing customisation of the message. It receives one to three arguments:
    • The commit message file path.
    • The contents of the --message or --template supplied to git commit, or merge or squash if it's a merge or squash commit.
    • The commit reference if amending an existing commit.
  • pre-commit is invoked before a commit is created, allowing verification of its contents.

Server-side hooks

  • pre-receive receives new objects and is able to determine whether or not to accept them.
  • post-receive is useful for triggering a build or deployment of a website with a static site generator.