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