Chapter 14. Hooks
Git hooks allow you to tweak standard Git operations. You can use a Git hook to run one or more arbitrary scripts whenever a particular event, such as a commit or a patch, occurs in your repository. Typically, an event is broken down into several prescribed steps, and you can tie a custom script to each step. When the Git event occurs, the appropriate script is called at the outset of each step.
Hooks belong to and affect a specific repository and are not copied during a clone operation. In other words, hooks you set up in your private repository are not propagated to and do not alter the behavior of the new clone. If, for some reason, your development process mandates hooks in each developerâs personal development repository, arrange to copy the directory .git/hooks through some other (non-clone) method.
Do not confuse this with initializing a new repository. When the git init
command is specified, Git copies available hooks to the new repository; these can include the default sample hooks Git provides or some custom hooks you specify in a configurable path defined via the template directory mechanism.
In this chapter, we start by discussing the available types of Git hooks and how to install them, then guide you through the process of creating a simple Git hook. We also explain how and when Git hooks can be used to alter standard Git operations.
Types of Hooks
A hook runs either in the context of your current, local repository or in the context of the remote ...
Get Version Control with Git, 3rd Edition 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.