Debian Codegraph @

docs/index.md

raw symbols SCIP
1 # Introduction
2 Gitlint is a git commit message linter written in python: it checks your commit messages for style.
3
4 Great for use as a [commit-msg git hook](#using-gitlint-as-a-commit-msg-hook) or as part of your gating script in a
5 [CI pipeline (e.g. Jenkins)](index.md#using-gitlint-in-a-ci-environment).
6
7 <script type="text/javascript" src="https://asciinema.org/a/30477.js" id="asciicast-30477" async></script>
8
9 !!! note
10 **Gitlint works on Windows**, but [there are some known issues](https://github.com/jorisroovers/gitlint/issues?q=is%3Aissue+is%3Aopen+label%3Awindows).
11
12 Also, gitlint is not the only git commit message linter out there, if you are looking for an alternative written in a different language,
13 have a look at [fit-commit](https://github.com/m1foley/fit-commit) (Ruby),
14 [node-commit-msg](https://github.com/clns/node-commit-msg) (Node.js) or [commitlint](http://marionebl.github.io/commitlint) (Node.js).
15
16
17 !!! important
18 **Gitlint requires Python 3.7 (or above). For Python 2.7 and Python 3.5 use `gitlint==0.14.0` (released 2020-10-24), for Python 3.6 `gitlint==0.18.0` (released 2022-11-16).**
19
20 ## Features
21 - **Commit message hook**: [Auto-trigger validations against new commit message right when you're committing](#using-gitlint-as-a-commit-msg-hook). Also [works with pre-commit](#using-gitlint-through-pre-commit).
22 - **Easily integrated**: Gitlint is designed to work [with your own scripts or CI system](#using-gitlint-in-a-ci-environment).
23 - **Sane defaults:** Many of gitlint's validations are based on
24 [well-known](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html),
25 [community](https://addamhardy.com/2013-06-05-good-commit-messages-and-enforcing-them-with-git-hooks),
26 [standards](http://chris.beams.io/posts/git-commit/), others are based on checks that we've found
27 useful throughout the years.
28 - **Easily configurable:** Gitlint has sane defaults, but [you can also easily customize it to your own liking](configuration.md).
29 - **Community contributed rules**: Conventions that are common but not universal [can be selectively enabled](contrib_rules.md).
30 - **User-defined rules:** Want to do more then what gitlint offers out of the box? Write your own [user defined rules](user_defined_rules.md).
31 - **Full unicode support:** Lint your Russian, Chinese or Emoji commit messages with ease!
32 - **Production-ready:** Gitlint checks a lot of the boxes you're looking for: actively maintained, high unit test coverage, integration tests,
33 python code standards ([black](https://github.com/psf/black), [ruff](https://github.com/charliermarsh/ruff)),
34 good documentation, widely used, proven track record.
35
36 ## Getting Started
37 ### Installation
38 ```sh
39 # Pip is recommended to install the latest version
40 pip install gitlint
41
42 # Alternative: by default, gitlint is installed with pinned dependencies.
43 # To install gitlint with looser dependency requirements, only install gitlint-core.
44 pip install gitlint-core
45
46 # Community maintained packages:
47 brew install gitlint # Homebrew (macOS)
48 sudo port install gitlint # Macports (macOS)
49 apt-get install gitlint # Ubuntu
50 # Other package managers, see https://repology.org/project/gitlint/versions
51
52 # Docker: https://hub.docker.com/r/jorisroovers/gitlint
53 docker run --ulimit nofile=1024 -v $(pwd):/repo jorisroovers/gitlint
54 # NOTE: --ulimit is required to work around a limitation in Docker
55 # Details: https://github.com/jorisroovers/gitlint/issues/129
56 ```
57
58 ### Usage
59 ```sh
60 # Check the last commit message
61 gitlint
62 # Alternatively, pipe a commit message to gitlint:
63 cat examples/commit-message-1 | gitlint
64 # or
65 git log -1 --pretty=%B | gitlint
66 # Or read the commit-msg from a file, like so:
67 gitlint --msg-filename examples/commit-message-2
68 # Lint all commits in your repo
69 gitlint --commits HEAD
70
71 # To install a gitlint as a commit-msg git hook:
72 gitlint install-hook
73 ```
74
75 Output example:
76 ```sh
77 $ cat examples/commit-message-2 | gitlint
78 1: T1 Title exceeds max length (134>80): "This is the title of a commit message that is over 80 characters and contains hard tabs and trailing whitespace and the word wiping "
79 1: T2 Title has trailing whitespace: "This is the title of a commit message that is over 80 characters and contains hard tabs and trailing whitespace and the word wiping "
80 1: T4 Title contains hard tab characters (\t): "This is the title of a commit message that is over 80 characters and contains hard tabs and trailing whitespace and the word wiping "
81 2: B4 Second line is not empty: "This line should not contain text"
82 3: B1 Line exceeds max length (125>80): "Lines typically need to have a max length, meaning that they can't exceed a preset number of characters, usually 80 or 120. "
83 3: B2 Line has trailing whitespace: "Lines typically need to have a max length, meaning that they can't exceed a preset number of characters, usually 80 or 120. "
84 3: B3 Line contains hard tab characters (\t): "Lines typically need to have a max length, meaning that they can't exceed a preset number of characters, usually 80 or 120. "
85 ```
86 !!! note
87 The returned exit code equals the number of errors found. [Some exit codes are special](index.md#exit-codes).
88
89 ### Shell completion
90
91 ```sh
92 # Bash: add to ~/.bashrc
93 eval "$(_GITLINT_COMPLETE=bash_source gitlint)"
94
95 # Zsh: add to ~/.zshrc
96 eval "$(_GITLINT_COMPLETE=zsh_source gitlint)"
97
98 # Fish: add to ~/.config/fish/completions/foo-bar.fish
99 eval (env _GITLINT_COMPLETE=fish_source gitlint)
100 ```
101
102 ## Configuration
103
104 For in-depth documentation of general and rule-specific configuration options, have a look at the [Configuration](configuration.md) and [Rules](rules.md) pages.
105
106 Short example `.gitlint` file ([full reference](configuration.md)):
107
108 ```ini
109 [general]
110 # Ignore certain rules (comma-separated list), you can reference them by
111 # their id or by their full name
112 ignore=body-is-missing,T3
113
114 # Ignore any data sent to gitlint via stdin
115 ignore-stdin=true
116
117 # Configure title-max-length rule, set title length to 80 (72 = default)
118 [title-max-length]
119 line-length=80
120
121 # You can also reference rules by their id (B1 = body-max-line-length)
122 [B1]
123 line-length=123
124 ```
125
126 Example use of flags:
127
128 ```sh
129 # Change gitlint's verbosity.
130 $ gitlint -v
131 # Ignore certain rules
132 $ gitlint --ignore body-is-missing,T3
133 # Enable debug mode
134 $ gitlint --debug
135 # Load user-defined rules (see http://jorisroovers.github.io/gitlint/user_defined_rules)
136 $ gitlint --extra-path /home/joe/mygitlint_rules
137 ```
138
139 Other commands and variations:
140
141 ```no-highlight
142 $ gitlint --help
143 Usage: gitlint [OPTIONS] COMMAND [ARGS]...
144
145 Git lint tool, checks your git commit messages for styling issues
146
147 Documentation: http://jorisroovers.github.io/gitlint
148
149 Options:
150 --target DIRECTORY Path of the target git repository. [default:
151 current working directory]
152 -C, --config FILE Config file location [default: .gitlint]
153 -c TEXT Config flags in format <rule>.<option>=<value>
154 (e.g.: -c T1.line-length=80). Flag can be
155 used multiple times to set multiple config values.
156 --commit TEXT Hash (SHA) of specific commit to lint.
157 --commits TEXT The range of commits (refspec or comma-separated
158 hashes) to lint. [default: HEAD]
159 -e, --extra-path PATH Path to a directory or python module with extra
160 user-defined rules
161 --ignore TEXT Ignore rules (comma-separated by id or name).
162 --contrib TEXT Contrib rules to enable (comma-separated by id or
163 name).
164 --msg-filename FILENAME Path to a file containing a commit-msg.
165 --ignore-stdin Ignore any stdin data. Useful for running in CI
166 server.
167 --staged Attempt smart guesses about meta info (like
168 author name, email, branch, changed files, etc)
169 for staged commits.
170 --fail-without-commits Hard fail when the target commit range is empty.
171 -v, --verbose Verbosity, more v's for more verbose output
172 (e.g.: -v, -vv, -vvv). [default: -vvv]
173 -s, --silent Silent mode (no output).
174 Takes precedence over -v, -vv, -vvv.
175 -d, --debug Enable debugging output.
176 --version Show the version and exit.
177 --help Show this message and exit.
178
179 Commands:
180 generate-config Generates a sample gitlint config file.
181 install-hook Install gitlint as a git commit-msg hook.
182 lint Lints a git repository [default command]
183 run-hook Runs the gitlint commit-msg hook.
184 uninstall-hook Uninstall gitlint commit-msg hook.
185
186 When no COMMAND is specified, gitlint defaults to 'gitlint lint'.
187 ```
188
189
190 ## Using gitlint as a commit-msg hook
191 _Introduced in gitlint v0.4.0_
192
193 You can also install gitlint as a git `commit-msg` hook so that gitlint checks your commit messages automatically
194 after each commit.
195
196 ```sh
197 gitlint install-hook
198 # To remove the hook
199 gitlint uninstall-hook
200 ```
201
202 !!! important
203
204 Gitlint cannot work together with an existing hook. If you already have a `.git/hooks/commit-msg`
205 file in your local repository, gitlint will refuse to install the `commit-msg` hook. Gitlint will also only
206 uninstall unmodified commit-msg hooks that were installed by gitlint.
207 If you're looking to use gitlint in conjunction with other hooks, you should consider
208 [using gitlint with pre-commit](#using-gitlint-through-pre-commit).
209
210 ## Using gitlint through [pre-commit](https://pre-commit.com)
211
212 `gitlint` can be configured as a plugin for the `pre-commit` git hooks
213 framework. Simply add the configuration to your `.pre-commit-config.yaml`:
214
215 ```yaml
216 - repo: https://github.com/jorisroovers/gitlint
217 rev: # Fill in a tag / sha here
218 hooks:
219 - id: gitlint
220 ```
221
222 You then need to install the pre-commit hook like so:
223 ```sh
224 pre-commit install --hook-type commit-msg
225 ```
226 !!! important
227
228 It's important that you run `pre-commit install --hook-type commit-msg`, even if you've already used
229 `pre-commit install` before. `pre-commit install` does **not** install commit-msg hooks by default!
230
231 To manually trigger gitlint using `pre-commit` for your last commit message, use the following command:
232 ```sh
233 pre-commit run gitlint --hook-stage commit-msg --commit-msg-filename .git/COMMIT_EDITMSG
234 ```
235
236 In case you want to change gitlint's behavior, you should either use a `.gitlint` file
237 (see [Configuration](configuration.md)) or modify the gitlint invocation in
238 your `.pre-commit-config.yaml` file like so:
239 ```yaml
240 - repo: https://github.com/jorisroovers/gitlint
241 rev: # Fill in a tag / sha here (e.g. v0.18.0)
242 hooks:
243 - id: gitlint
244 args: [--contrib=CT1, --msg-filename]
245 ```
246
247 !!! important
248
249 You need to add `--msg-filename` at the end of your custom `args` list as the gitlint-hook will fail otherwise.
250
251
252 ### gitlint and pre-commit in CI
253 gitlint also supports a `gitlint-ci` pre-commit hook that can be used in CI environments.
254
255 Configure it like so:
256 ```yaml
257 - repo: https://github.com/jorisroovers/gitlint
258 rev: # insert ref, e.g. v0.18.0
259 hooks:
260 - id: gitlint # this is the regular commit-msg hook
261 - id: gitlint-ci # hook for CI environments
262 ```
263
264 And invoke it in your CI environment like this:
265
266 ```sh
267 pre-commit run --hook-stage manual gitlint-ci
268 ```
269
270 By default this will only lint the latest commit.
271 If you want to lint more commits you can modify the `gitlint-ci` hook like so:
272
273 ```yaml
274 - repo: https://github.com/jorisroovers/gitlint
275 rev: # insert ref, e.g. v0.18.0
276 hooks:
277 - id: gitlint
278 - id: gitlint-ci
279 args: [--debug, --commits, mybranch] # enable debug mode, lint all commits in mybranch
280 ```
281
282 ## Using gitlint in a CI environment
283 By default, when just running `gitlint` without additional parameters, gitlint lints the last commit in the current
284 working directory.
285
286 This makes it easy to use gitlint in a CI environment (Jenkins, TravisCI, Github Actions, pre-commit, CircleCI, Gitlab, etc).
287 In fact, this is exactly what we do ourselves: on every commit,
288 [we run gitlint as part of our CI checks](https://github.com/jorisroovers/gitlint/blob/v0.12.0/run_tests.sh#L133-L134).
289 This will cause the build to fail when we submit a bad commit message.
290
291 Alternatively, gitlint will also lint any commit message that you feed it via stdin like so:
292 ```sh
293 # lint the last commit message
294 git log -1 --pretty=%B | gitlint
295 # lint a specific commit: 62c0519
296 git log -1 --pretty=%B 62c0519 | gitlint
297 ```
298 Note that gitlint requires that you specify `--pretty=%B` (=only print the log message, not the metadata),
299 future versions of gitlint might fix this and not require the `--pretty` argument.
300
301 ## Linting specific commits or branches
302
303 Gitlint can lint specific commits using `--commit`:
304 ```sh
305 gitlint --commit 019cf40580a471a3958d3c346aa8bfd265fe5e16
306 gitlint --commit 019cf40 # short SHAs work too
307 gitlint --commit HEAD~2 # as do special references
308 gitlint --commit mybranch # lint latest commit on a branch
309 ```
310
311 You can also lint multiple commits using `--commits` (plural):
312
313 ```sh
314 # Lint a specific commit range:
315 gitlint --commits "019cf40...d6bc75a"
316 # Lint all commits on a branch
317 gitlint --commits mybranch
318 # Lint all commits that are different between a branch and your main branch
319 gitlint --commits "main..mybranch"
320 # Use git's special references
321 gitlint --commits "origin/main..HEAD"
322
323 # You can also pass multiple, comma separated commit hashes:
324 gitlint --commits 019cf40,c50eb150,d6bc75a
325 # These can include special references as well
326 gitlint --commits HEAD~1,mybranch-name,origin/main,d6bc75a
327 # You can also lint a single commit with --commits:
328 gitling --commits 019cf40,
329 ```
330
331 The `--commits` flag takes a **single** refspec argument or commit range. Basically, any range that is understood
332 by [git rev-list](https://git-scm.com/docs/git-rev-list) as a single argument will work.
333
334 Alternatively, you can pass `--commits` a comma-separated list of commit hashes (both short and full-length SHAs work,
335 as well as special references such as `HEAD` and branch names).
336 Gitlint will treat these as pointers to **single** commits and lint these in the order you passed.
337 `--commits` also accepts a single commit SHA with a trailing comma.
338
339 For cases where the `--commits` option doesn't provide the flexibility you need, you can always use a simple shell
340 script to lint an arbitrary set of commits, like shown in the example below.
341
342 ```sh
343 #!/bin/sh
344
345 for commit in $(git rev-list my-branch); do
346 echo "Commit $commit"
347 gitlint --commit $commit
348 echo "--------"
349 done
350 ```
351
352 !!! note
353 One downside to this approach is that you invoke gitlint once per commit vs. once per set of commits.
354 This means you'll incur the gitlint startup time once per commit, making it rather slow if you want to
355 lint a large set of commits. Always use `--commits` if you can to avoid this performance penalty.
356
357
358 ## Merge, fixup, squash and revert commits
359 _Introduced in gitlint v0.7.0 (merge), v0.9.0 (fixup, squash), v0.13.0 (revert) and v0.18.0 (fixup=amend)_
360
361 **Gitlint ignores merge, revert, fixup, and squash commits by default.**
362
363 For merge and revert commits, the rationale for ignoring them is
364 that most users keep git's default messages for these commits (i.e *Merge/Revert "[original commit message]"*).
365 Often times these commit messages are also auto-generated through tools like github.
366 These default/auto-generated commit messages tend to cause gitlint violations.
367 For example, a common case is that *"Merge:"* being auto-prepended triggers a
368 [title-max-length](rules.md#t1-title-max-length) violation. Most users don't want this, so we disable linting
369 on Merge and Revert commits by default.
370
371 For [squash](https://git-scm.com/docs/git-commit#git-commit---squashltcommitgt) and [fixup](https://git-scm.com/docs/git-commit#git-commit---fixupltcommitgt) (including [fixup=amend](https://git-scm.com/docs/git-commit#Documentation/git-commit.txt---fixupamendrewordltcommitgt)) commits, the rationale is that these are temporary
372 commits that will be squashed into a different commit, and hence the commit messages for these commits are very
373 short-lived and not intended to make it into the final commit history. In addition, by prepending *"fixup!"*,
374 *"amend!"* or *"squash!"* to your commit message, certain gitlint rules might be violated
375 (e.g. [title-max-length](rules.md#t1-title-max-length)) which is often undesirable.
376
377 In case you *do* want to lint these commit messages, you can disable this behavior by setting the
378 general `ignore-merge-commits`, `ignore-revert-commits`, `ignore-fixup-commits`, `ignore-fixup-amend-commits` or
379 `ignore-squash-commits` option to `false`
380 [using one of the various ways to configure gitlint](configuration.md).
381
382 ## Ignoring commits
383
384 You can configure gitlint to ignore specific commits or parts of a commit.
385
386 One way to do this, is by [adding a gitlint-ignore line to your commit message](configuration.md#commit-specific-config).
387
388 If you have a case where you want to ignore a certain type of commits all-together, you can
389 use gitlint's *ignore* rules.
390 Here's a few examples snippets from a `.gitlint` file:
391
392 ```ini
393 [ignore-by-title]
394 # Match commit titles starting with Release
395 regex=^Release(.*)
396 ignore=title-max-length,body-min-length
397 # ignore all rules by setting ignore to 'all'
398 # ignore=all
399
400 [ignore-by-body]
401 # Match commits message bodies that have a line that contains 'release'
402 regex=(.*)release(.*)
403 ignore=all
404
405 [ignore-by-author-name]
406 # Match commits by author name (e.g. ignore all rules when a commit is made by dependabot)
407 regex=dependabot
408 ignore=all
409 ```
410
411 If you just want to ignore certain lines in a commit, you can do that using the
412 [ignore-body-lines](rules.md#i3-ignore-body-lines) rule.
413
414 ```ini
415 # Ignore all lines that start with 'Co-Authored-By'
416 [ignore-body-lines]
417 regex=^Co-Authored-By
418 ```
419
420 !!! warning
421
422 When ignoring specific lines, gitlint will no longer be aware of them while applying other rules.
423 This can sometimes be confusing for end-users, especially as line numbers of violations will typically no longer
424 match line numbers in the original commit message. Make sure to educate your users accordingly.
425
426 !!! note
427
428 If you want to implement more complex ignore rules according to your own logic, you can do so using [user-defined
429 configuration rules](user_defined_rules.md#configuration-rules).
430
431 ## Named Rules
432
433 Introduced in gitlint v0.14.0
434
435 Named rules allow you to have multiple of the same rules active at the same time, which allows you to
436 enforce the same rule multiple times but with different options. Named rules are so-called because they require an
437 additional unique identifier (i.e. the rule *name*) during configuration.
438
439 !!! warning
440
441 Named rules is an advanced topic. It's easy to make mistakes by defining conflicting instances of the same rule.
442 For example, by defining 2 `body-max-line-length` rules with different `line-length` options, you obviously create
443 a conflicting situation. Gitlint does not do any resolution of such conflicts, it's up to you to make sure
444 any configuration is non-conflicting. So caution advised!
445
446 Defining a named rule is easy, for example using your `.gitlint` file:
447
448 ```ini
449 # By adding the following section, you will add a second instance of the
450 # title-must-not-contain-word (T5) rule (in addition to the one that is enabled
451 # by default) with the name 'extra-words'.
452 [title-must-not-contain-word:extra-words]
453 words=foo,bar
454
455 # So the generic form is
456 # [<rule-id-or-name>:<your-chosen-name>]
457 # Another example, referencing the rule type by id
458 [T5:more-words]
459 words=hur,dur
460
461 # You can add as many additional rules and you can name them whatever you want
462 # The only requirement is that names cannot contain whitespace or colons (:)
463 [title-must-not-contain-word:This-Can_Be*Whatever$YouWant]
464 words=wonderwoman,batman,power ranger
465 ```
466
467 When executing gitlint, you will see the violations from the default `title-must-not-contain-word (T5)` rule, as well as
468 the violations caused by the additional Named Rules.
469
470 ```sh
471 $ gitlint
472 1: T5 Title contains the word 'WIP' (case-insensitive): "WIP: foo wonderwoman hur bar"
473 1: T5:This-Can_Be*Whatever$YouWant Title contains the word 'wonderwoman' (case-insensitive): "WIP: foo wonderwoman hur bar"
474 1: T5:extra-words Title contains the word 'foo' (case-insensitive): "WIP: foo wonderwoman hur bar"
475 1: T5:extra-words Title contains the word 'bar' (case-insensitive): "WIP: foo wonderwoman hur bar"
476 1: T5:more-words Title contains the word 'hur' (case-insensitive): "WIP: foo wonderwoman hur bar"
477 ```
478
479 Named rules are further treated identical to all other rules in gitlint:
480
481 - You can reference them by their full name, when e.g. adding them to your `ignore` configuration
482 ```ini
483 # .gitlint file example
484 [general]
485 ignore=T5:more-words,title-must-not-contain-word:extra-words
486 ```
487
488 - You can use them to instantiate multiple of the same [user-defined rule](user_defined_rules.md)
489 - You can configure them using [any of the ways you can configure regular gitlint rules](configuration.md)
490
491
492 ## Exit codes
493 Gitlint uses the exit code as a simple way to indicate the number of violations found.
494 Some exit codes are used to indicate special errors as indicated in the table below.
495
496 Because of these special error codes and the fact that
497 [bash only supports exit codes between 0 and 255](http://tldp.org/LDP/abs/html/exitcodes.html), the maximum number
498 of violations counted by the exit code is 252. Note that gitlint does not have a limit on the number of violations
499 it can detect, it will just always return with exit code 252 when the number of violations is greater than or equal
500 to 252.
501
502 | Exit Code | Description |
503 | --------- | ------------------------------------------ |
504 | 253 | Wrong invocation of the `gitlint` command. |
505 | 254 | Something went wrong when invoking git. |
506 | 255 | Invalid gitlint configuration |