Git hooks

Git hooks are scripts that are automatically executed when certain events occur in a Git repository, including:

They can be located in either local or server-side repositories, allowing you to customise Git repositories and trigger user-defined actions.

Git hooks are located in the .git/hooks/ directory. When a repository is created, some sample scripts are already created there:

.git/hooks
├── applypatch-msg.sample
├── commit-msg.sample
├── fsmonitor-watchman.sample
├── post-update.sample
├── pre-applypatch.sample
├── pre-commit.sample
├── pre-merge-commit.sample
├── pre-push.sample
├── pre-rebase.sample
├── pre-receive.sample
├── prepare-commit-msg.sample
├── push-to-checkout.sample
├── sendemail-validate.sample
└── update.sample

For the scripts to be executed, only the suffix .sample must be removed and, if necessary, the file permission must be executable, for example with chmod +x .git/PREPARE-COMMIT-MSG.

The integrated scripts are shell and Perl scripts, but any scripting language can be used. The Shebang line (#!/bin/sh) determines how the file is to be interpreted.

However, the scripts are not copied to the Git server using git push. To be able to use scripts across multiple repositories, the pre-commit framework is therefore recommended.

See also

Configuration-based hooks

Added in version 2.54: Git 2.54 now introduces a new way to define hooks in your configuration files: instead of placing a script in .git/hooks/pre-commit, you can now specify the following:

[hook "ruff check"]
   event = pre-commit
   command = ~/bin/ruff check --fix --exit-non-zero-on-fix

However, this configuration can be specified not only for each project, but also globally or system-wide in ~/.gitconfig or in /etc/gitconfig.

Use git hook list pre-commit to find out which hooks are configured and where they come from.