
---
url: /mod/githook.md
---
# x githook

Git Hooks management
## Introduction



Use YAML declarative configuration files to manage Git Hooks for your project.



Git Hooks: These are custom scripts that can be defined to run during Git operations, such as checking code style, data lint, and more, before committing.



While developing the [gitconfig](/mod/gitconfig.md) module, we noticed that the way Git Hooks are managed varies across different language ecosystems. For example, Node.js has Husky, Python has pre-commit, and these tools often rely on language runtimes and have different configuration and usage methods.



In this context, we have extracted Git Hooks management from the gitconfig module and created the githook module. This module aims to provide a more universal and convenient way to manage Git Hooks, offering an out-of-the-box Git Hooks management tool without the need for additional package downloads.



## Usage


1. Create a declarative configuration file named `.x-cmd/git/hook.yml` in the project's root directory.
2. Write Git Hooks commands in the configuration file. For example:
```yml
pre-commit: echo "pre-commit"
post-commit: |
  echo "post-commit-1"
  echo "post-commit-2"
```
3. Run the command to apply changes: `x githook apply`



## Automatic Configuration `apply`



After running the `x githook apply` or `x gitconfig apply` command for the first time in your project, if you make changes to the hook configuration, it will automatically update the hook configuration based on file modification times before triggering the hook.



:::info

For efficiency reasons, x-cmd is not loaded in the hook execution environment.



If you want to use `x` in the hook environment, you can load it using the following method:
```yml
pre-commit: |
  . "$HOME/.x-cmd.root/X"
  x cowsay "pre-commit"
```

:::



## Configure After Cloning the Repository



When using `x git clone`, `x gh cl`, `x gt cl`, and other x-cmd git series CLI commands to clone a project, it will trigger an `apply` to apply configurations.

## Sub Commands

| Name | Description |
| ------- | ------- |
| [x githook apply](/mod/githook/apply.md) | apply a yml configure file |
| [x githook clear](/mod/githook/clear.md) | clear hooks files and unset configure |
