| 1 |
This feature checks whether a string or a range of git commits follows the given committing rules. Comments in git messages will be ignored. |
| 2 |
|
| 3 |
To set up an automatic check before every git commit, please refer to [Automatically check message before commit](../tutorials/auto_check.md). |
| 4 |
|
| 5 |
## Usage |
| 6 |
|
| 7 |
 |
| 8 |
|
| 9 |
More specifically, there are three mutually exclusive ways to use `cz check`: |
| 10 |
|
| 11 |
- Validate a range of git commit messages with `--rev-range` |
| 12 |
- Validate a given string with `--message` or by piping the message to it |
| 13 |
- Validate a commit message from a file with `--commit-msg-file` |
| 14 |
|
| 15 |
### Use `cz check` to validate a commit message before committing |
| 16 |
|
| 17 |
#### Option 1: use `--message` to check a given string: |
| 18 |
|
| 19 |
```bash |
| 20 |
cz check --message <message_to_be_checked> |
| 21 |
``` |
| 22 |
|
| 23 |
#### Option 2: pipe the message to `cz check`: |
| 24 |
```bash |
| 25 |
echo <message_to_be_checked> | cz check |
| 26 |
``` |
| 27 |
|
| 28 |
#### Option 3: use `--commit-msg-file` to read the commit message from a file |
| 29 |
```bash |
| 30 |
cz check --commit-msg-file /path/to/file.txt |
| 31 |
``` |
| 32 |
|
| 33 |
## Command Line Options |
| 34 |
|
| 35 |
### `--rev-range` |
| 36 |
|
| 37 |
Test if a given range of commits in the git log passes `cz check`. |
| 38 |
|
| 39 |
```bash |
| 40 |
cz check --rev-range REV_RANGE |
| 41 |
``` |
| 42 |
|
| 43 |
For more information on `REV_RANGE`, check the [git documentation](https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection#_commit_ranges). |
| 44 |
|
| 45 |
#### Use cases |
| 46 |
|
| 47 |
1. Validate the latest 3 commit messages: |
| 48 |
```bash |
| 49 |
cz check --rev-range HEAD~3..HEAD |
| 50 |
# or |
| 51 |
cz check --rev-range HEAD~3.. |
| 52 |
# or |
| 53 |
cz check --rev-range HEAD~~~.. |
| 54 |
``` |
| 55 |
|
| 56 |
1. Validate all git commit messages on some branch up to HEAD: |
| 57 |
```bash |
| 58 |
cz check --rev-range <branch_name>..HEAD |
| 59 |
``` |
| 60 |
|
| 61 |
For example, to check all git commit messages on `main` branch up to HEAD: |
| 62 |
```bash |
| 63 |
cz check --rev-range main..HEAD |
| 64 |
``` |
| 65 |
|
| 66 |
or if your project still uses `master` branch: |
| 67 |
```bash |
| 68 |
cz check --rev-range master..HEAD |
| 69 |
``` |
| 70 |
|
| 71 |
!!! note "Default branch" |
| 72 |
Usually the default branch is `main` or `master`. |
| 73 |
You can check the default branch by running `cz check --use-default-range`. |
| 74 |
|
| 75 |
1. Validate all git commit messages starting from when you first implemented commit message linting: |
| 76 |
|
| 77 |
**(Why this is useful?)** Let's say you decided to enforce commit message today. However, it is impractical to `git rebase` all your previous commits. `--rev-range` helps you skip commits before you first implemented commit message linting by using a specific commit hash. |
| 78 |
|
| 79 |
```bash |
| 80 |
cz check --rev-range <first_commit_sha>..HEAD |
| 81 |
``` |
| 82 |
|
| 83 |
### `--use-default-range` |
| 84 |
|
| 85 |
Equivalent to `--rev-range <default_branch>..HEAD`. |
| 86 |
|
| 87 |
```bash |
| 88 |
cz check --use-default-range |
| 89 |
# or |
| 90 |
cz check -d |
| 91 |
``` |
| 92 |
|
| 93 |
### `--message` |
| 94 |
|
| 95 |
Test if a given string passes `cz check`. |
| 96 |
|
| 97 |
```bash |
| 98 |
cz check --message <message_to_be_checked> |
| 99 |
``` |
| 100 |
|
| 101 |
### `--commit-msg-file` |
| 102 |
|
| 103 |
Test if a given file contains a commit message that passes `cz check`. |
| 104 |
|
| 105 |
```bash |
| 106 |
cz check --commit-msg-file <path_to_file_containing_message_to_be_checked> |
| 107 |
``` |
| 108 |
|
| 109 |
This can be useful when cooperating with git hooks. Please check [Automatically check message before commit](../tutorials/auto_check.md) for more detailed examples. |
| 110 |
|
| 111 |
### `--allow-abort` |
| 112 |
|
| 113 |
Example: |
| 114 |
|
| 115 |
```bash |
| 116 |
cz check --message <message_to_be_checked> --allow-abort |
| 117 |
``` |
| 118 |
|
| 119 |
Empty commit messages typically instruct Git to abort a commit, so you can pass `--allow-abort` to |
| 120 |
permit them. Since `git commit` accepts the `--allow-empty-message` flag (primarily for wrapper scripts), you may wish to disallow such commits in CI. `--allow-abort` may be used in conjunction with any of the other options. |
| 121 |
|
| 122 |
### `--allowed-prefixes` |
| 123 |
|
| 124 |
Skip validation for commit messages that start with the specified prefixes. |
| 125 |
|
| 126 |
If not set, commit messages starting with the following prefixes are ignored by `cz check`: |
| 127 |
|
| 128 |
- `Merge` |
| 129 |
- `Revert` |
| 130 |
- `Pull request` |
| 131 |
- `fixup!` |
| 132 |
- `squash!` |
| 133 |
- `amend!` |
| 134 |
|
| 135 |
```bash |
| 136 |
cz check --message <message_to_be_checked> --allowed-prefixes 'Merge' 'Revert' 'Custom Prefix' |
| 137 |
``` |
| 138 |
|
| 139 |
For example, |
| 140 |
|
| 141 |
```bash |
| 142 |
# The following message passes the check because it starts with 'Merge' |
| 143 |
cz check --message "Merge branch 'main' into feature/new-feature" --allowed-prefixes 'Merge' |
| 144 |
|
| 145 |
# The following fails |
| 146 |
cz check --message "Merge branch 'main' into feature/new-feature" --allowed-prefixes 'aaa' |
| 147 |
``` |
| 148 |
|
| 149 |
### `--message-length-limit` |
| 150 |
|
| 151 |
Restrict the length of **the first line** of the commit message. |
| 152 |
|
| 153 |
```bash |
| 154 |
# The following passes |
| 155 |
cz check --message "docs(check): fix grammar issues" -l 80 |
| 156 |
|
| 157 |
# The following fails |
| 158 |
cz check --message "docs:very long long long long message with many words" -l 3 |
| 159 |
``` |
| 160 |
|
| 161 |
By default, the limit is set to `0`, which means no limit on the length. |
| 162 |
|
| 163 |
!!! note |
| 164 |
Specifically, for `ConventionalCommitsCz` the length only counts from the type of change to the subject, while the body and the footer are not counted. |