Available Hooks

As Git evolves, new hooks become available. To discover what hooks are available in your version of Git, run git help hooks. Also, refer to the Git documentation to find all the command-line parameters as well as the input and output of each hook.

Commit-Related Hooks

When you run git commit, Git executes a process like that shown in Figure 14-1.

Commit hook processing

Figure 14-1. Commit hook processing

Warning

None of the commit hooks run for anything other than git commit. For example, git rebase, git merge, and git am don’t run your commit hooks by default. (Those commands may run other hooks, though.) However, git commit --amend does run your commit hooks.

Each hook has its own purpose:

  • The pre-commit hook gives you the chance to immediately abort a commit if something is wrong with the content being committed. The pre-commit hook runs before the user is allowed to edit the commit message, so the user won’t enter a commit message only to discover the changes are rejected. You can also use this hook to automatically modify the content of the commit.

  • prepare-commit-msg lets you modify Git’s default message before it is shown to the user. For example, you can use this to change the default commit message template.

  • The commit-msg hook can validate or modify the commit message after the user edits it. For example, you can leverage this hook to check for spelling mistakes or reject messages ...

Get Version Control with 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.