Chapter 15Extending Git Functionality with Git Hooks

Now that I’ve covered the overall Git workflow and functionality in detail, let’s look at one way you can extend and customize how Git works: hooks. Hooks are scripts or programs that run before or after (and in some cases during) a subset of operations in Git. There are hooks for local operations, such as commits or merges, and hooks for remote operations, such as when changes are pushed to the remote.

In some other source management systems, hooks may be called by other names, such as triggers. However, the concept is the same—a program or script runs when a certain event or operation happens. Here are some common uses for hooks:

  • Sending e-mail or other notifications when a change is pushed to a repository
  • Validating that certain conditions have been met before a commit
  • Appending items to commit messages
  • Checking the format or existence of certain elements in a commit message
  • Updating content in the working directory after an operation
  • Enforcing coding standards

INSTALLING HOOKS

By default, hooks exist in the hooks directory underneath your Git directory. Your Git directory is directly under your working directory unless you’ve overridden that location by setting a different value in the $GIT_DIR environment variable, ...

Get Professional Git now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.