| 1 |
# Automatically check message before commit |
| 2 |
|
| 3 |
## About |
| 4 |
|
| 5 |
To automatically check a commit message before committing, use a [Git hook](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks). This ensures all commit messages match your project's commitizen format before they are accepted into the repository. |
| 6 |
|
| 7 |
When a commit message fails validation, Git rejects the commit and displays an error explaining what went wrong. Update the message to the required format before trying again. |
| 8 |
|
| 9 |
## How to |
| 10 |
|
| 11 |
There are two common methods for installing the hooks: |
| 12 |
|
| 13 |
### Method 1: Using pre-commit hook frameworks (Recommended) |
| 14 |
|
| 15 |
Using pre-commit hook frameworks is the recommended approach because hook installation, updates, and execution are handled automatically. |
| 16 |
Two common frameworks are: |
| 17 |
|
| 18 |
1. [prek](https://prek.j178.dev) (faster) |
| 19 |
2. [pre-commit](https://pre-commit.com/) |
| 20 |
|
| 21 |
|
| 22 |
In the steps below, we'll use `prek`. |
| 23 |
|
| 24 |
|
| 25 |
!!! tip "Using pre-commit framework" |
| 26 |
If you use pre-commit instead of prek, you can run the same commands. Simply replace prek with pre-commit in the steps below. |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#### Step 1: Install prek |
| 31 |
|
| 32 |
```sh |
| 33 |
python -m pip install prek |
| 34 |
``` |
| 35 |
|
| 36 |
#### Step 2: Create `.pre-commit-config.yaml` |
| 37 |
|
| 38 |
Create a `.pre-commit-config.yaml` file in your project root directory with the following content: |
| 39 |
|
| 40 |
```yaml |
| 41 |
--- |
| 42 |
repos: |
| 43 |
- repo: https://github.com/commitizen-tools/commitizen |
| 44 |
rev: v4.10.0 # Use the latest version or a specific version |
| 45 |
hooks: |
| 46 |
- id: commitizen |
| 47 |
stages: [commit-msg] |
| 48 |
``` |
| 49 |
|
| 50 |
!!! tip "Using the latest version" |
| 51 |
Replace `v4.10.0` with the latest commitizen version. You can find the latest version on [GitHub releases](https://github.com/commitizen-tools/commitizen/releases) or use a specific commit SHA for pinning to an exact version. |
| 52 |
|
| 53 |
#### Step 3: Install the hook |
| 54 |
|
| 55 |
Install the configuration into Git's hook system: |
| 56 |
|
| 57 |
```bash |
| 58 |
prek install --hook-type commit-msg |
| 59 |
``` |
| 60 |
|
| 61 |
The hook is now active! Every time you create a commit, commitizen will automatically validate your commit message. |
| 62 |
|
| 63 |
### Method 2: Manual Git hook installation |
| 64 |
|
| 65 |
If you prefer not to use a pre-commit framework, you can manually create a Git hook. This gives you full control over the hook script but requires manual maintenance. |
| 66 |
|
| 67 |
#### Step 1: Create the commit-msg hook |
| 68 |
|
| 69 |
Navigate to your project's `.git/hooks` directory and create the `commit-msg` hook file: |
| 70 |
|
| 71 |
```bash |
| 72 |
cd .git/hooks |
| 73 |
touch commit-msg |
| 74 |
chmod +x commit-msg |
| 75 |
``` |
| 76 |
|
| 77 |
#### Step 2: Add the commitizen check command |
| 78 |
|
| 79 |
Open the `commit-msg` file in your editor and add the following content: |
| 80 |
|
| 81 |
```bash |
| 82 |
#!/bin/bash |
| 83 |
cz check --allow-abort --commit-msg-file $1 |
| 84 |
``` |
| 85 |
|
| 86 |
Where: |
| 87 |
|
| 88 |
- `$1` is the temporary file path that Git provides, containing the current commit message |
| 89 |
- `--allow-abort` allows empty commit messages (which typically instruct Git to abort a commit) |
| 90 |
- `--commit-msg-file` tells commitizen to read the commit message from the specified file |
| 91 |
|
| 92 |
The hook is now active! Each time you create a commit, this hook will automatically validate your commit message. |
| 93 |
|
| 94 |
## Testing the hook |
| 95 |
|
| 96 |
After installing the hook, you can test it by attempting to commit with an invalid message: |
| 97 |
|
| 98 |
```bash |
| 99 |
# This should fail with an invalid message |
| 100 |
git commit -m "invalid commit message" |
| 101 |
|
| 102 |
# This should succeed with a valid message |
| 103 |
git commit -m "feat: add new feature" |
| 104 |
``` |
| 105 |
|
| 106 |
If the hook is working correctly, invalid commit messages are rejected with an error explaining what's wrong. |
| 107 |
|
| 108 |
## What happens when validation fails? |
| 109 |
|
| 110 |
When a commit message fails validation: |
| 111 |
|
| 112 |
1. Git will abort the commit |
| 113 |
2. An error message will be displayed showing: |
| 114 |
- Which commit failed (if checking multiple commits) |
| 115 |
- The invalid commit message |
| 116 |
- The expected pattern/format |
| 117 |
3. Your changes remain staged, so you can simply amend the commit message and try again |
| 118 |
|
| 119 |
Example error output: |
| 120 |
|
| 121 |
<!-- DEPENDENCY: InvalidCommitMessageError --> |
| 122 |
|
| 123 |
``` |
| 124 |
commit validation: failed! |
| 125 |
please enter a commit message in the commitizen format. |
| 126 |
commit "abc123": "invalid message" |
| 127 |
pattern: ^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)(\(.+\))?: .{1,} |
| 128 |
``` |
| 129 |
|
| 130 |
## Troubleshooting |
| 131 |
|
| 132 |
### Hook not running |
| 133 |
|
| 134 |
- **Verify the hook file exists**: Check that `.git/hooks/commit-msg` exists and is executable |
| 135 |
- **Check file permissions**: Ensure the hook has execute permissions (`chmod +x .git/hooks/commit-msg`) |
| 136 |
- **Verify commitizen is installed**: Run `cz --version` to confirm commitizen is available in your PATH |
| 137 |
- **Check Git version**: Ensure you're using a recent version of Git that supports hooks |
| 138 |
|
| 139 |
### Prek hook not working |
| 140 |
|
| 141 |
- **Verify installation**: Run `prek --version` to confirm pre-commit is installed |
| 142 |
- **Reinstall the hook**: Try running `prek install --hook-type commit-msg` again |
| 143 |
- **Check configuration**: Verify your `.pre-commit-config.yaml` file is valid YAML and in the project root |
| 144 |
- **Update hooks**: Run `prek autoupdate` to update to the latest versions |
| 145 |
|
| 146 |
### Bypassing the hook (when needed) |
| 147 |
|
| 148 |
If you need to bypass the hook temporarily (e.g., for merge commits or special cases), you can use: |
| 149 |
|
| 150 |
```bash |
| 151 |
git commit --no-verify -m "your message" |
| 152 |
``` |
| 153 |
|
| 154 |
!!! warning "Use with caution" |
| 155 |
Only bypass hooks when absolutely necessary, as it defeats the purpose of automated validation. |
| 156 |
|
| 157 |
## Additional resources |
| 158 |
|
| 159 |
- Learn more about [Git hooks](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks) |
| 160 |
- See the [check command documentation](../commands/check.md) for more validation options |
| 161 |
- Check out [prek documentation](https://prek.j178.dev/) for advanced hook management |
| 162 |
- Check out [pre-commit documentation](https://pre-commit.com/) for advanced hook management |