Git Hooks is a flexible and powerful tool for managing and executing Git hooks across multiple levels of your development environment. It allows for global, local repository-specific, Husky, and standard Git hook configurations, providing a hierarchical approach to Git hook management.
- Configure global Git hooks in
~/.git-hooks - Support for local repository-specific hooks in
$GIT_DIR/.git-hooks - Support for Husky hooks in
.huskyfolder - Backwards compatibility with standard Git hooks
- Hierarchical execution of hooks (global → local → Husky → standard)
- Easy setup of specific hooks (e.g., gitleaks for pre-commit)
You can install Git Hooks using either Go's install command or Homebrew.
To install Git Hooks using Go's install command:
go install github.com/lvrach/git-hooks@latestThis command will download the latest version of Git Hooks, compile it, and install the binary in your $GOPATH/bin directory. Make sure your $GOPATH/bin is in your system's PATH to run git-hooks from any location.
If you haven't set GOPATH, the binary will typically be installed in $HOME/go/bin on Unix systems or %USERPROFILE%\go\bin on Windows.
To install Git Hooks using Homebrew:
brew install lvrach/tap/git-hooksThis command will install Git Hooks from the lvrach tap, making it available system-wide.
To configure Git to use Git Hooks:
git-hooks configThis command will:
- Create the
~/.git-hooksdirectory - Set up scripts for all Git hook types in this directory
- Configure Git to use this directory for hooks
To revert the changes made by the git-hooks config command:
git-hooks implodeOnce configured, Git Hooks will automatically handle Git hooks. When Git triggers a hook, it will run the corresponding script in ~/.git-hooks, which in turn calls git-hooks hook <hook-name>.
To set up supported hooks, use the add command. For example, to set up the gitleaks pre-commit hook:
git-hooks add gitleaksThis will create a pre-commit hook on a global level that runs gitleaks to check for sensitive information in your commits.
You can add custom hook scripts in the following locations:
- Global hooks:
~/.git-hooks/<hook-name>.d/ - Local repository hooks:
$GIT_DIR/.git-hooks/<hook-name>.d/
These scripts will be executed in order when the corresponding hook is triggered.
When a Git hook is triggered, Git Hooks executes hooks in the following order:
- Global hooks in
~/.git-hooks/<hook-name>.d/ - Local repository hooks in
$GIT_DIR/.git-hooks/<hook-name>.d/ - Husky hooks in
.husky/<hook-name>/ - Standard Git hook in
$GIT_DIR/hooks/<hook-name>
This order ensures that you can have a cascading set of hooks, from the most global to the most specific, with Husky integration for projects that use it.