Debian Codegraph @

docs/commands/bump.md

raw
1 ![Bump version](../images/cli_interactive/bump.gif)
2
3 ## About
4
5 `cz bump` is a powerful command that **automatically** determines and increases your project's version number based on your commit history.
6
7 It analyzes your commits to determine the appropriate version increment according to semantic versioning principles.
8
9 !!! note
10 In the following documentation, the term "configuration file" refers to `pyproject.toml`, `.cz.toml` or other configuration files.
11
12 We will use `pyproject.toml` as the configuration file throughout the documentation.
13
14 See [Configuration file](../config/configuration_file.md) for more details.
15
16 ## Key Features
17
18 - **Automatic Version Detection**: Analyzes commit history to determine the appropriate version bump
19 - **Manual Version Control**: Supports manual version specification when needed
20 - **Pre-release Support**: Handles alpha, beta, and release candidate versions
21 - **Multiple Version Schemes**: Supports both [PEP 440][pep440] and [semantic versioning][semver] formats
22
23 ### Version Increment Rules
24
25 The version follows the `MAJOR.MINOR.PATCH` format, with increments determined by your commit types:
26
27 | Increment | Description | Conventional commit map |
28 | --------- | --------------------------- | ----------------------- |
29 | `MAJOR` | Breaking changes introduced | `BREAKING CHANGE`, bang (e.g. `feat!`)|
30 | `MINOR` | New features | `feat` |
31 | `PATCH` | Fixes and improvements | `fix`, `perf`, `refactor`|
32
33 ## Command line options
34
35 ![cz bump --help](../images/cli_help/cz_bump___help.svg)
36
37 ### `--version-files-only`
38
39 Bumps the version in the files defined in [`version_files`][version_files] without creating a commit and tag on the git repository.
40
41 ```bash
42 cz bump --version-files-only
43 ```
44
45 ### `--changelog`
46
47 Generate a **changelog** along with the new version and tag when bumping. See [changelog](./changelog.md) for more details.
48
49 ```bash
50 cz bump --changelog
51 ```
52
53 ### `--prerelease`
54
55 The bump is a pre-release bump, meaning that in addition to a possible version bump the new version receives a
56 pre-release segment compatible with the bump's version scheme, where the segment consists of a _phase_ and a
57 non-negative number. Supported options for `--prerelease` are the following phase names `alpha`, `beta`, or
58 `rc` (release candidate). For more details, refer to the
59 [Python Packaging User Guide](https://packaging.python.org/en/latest/specifications/version-specifiers/#pre-releases).
60
61 Note that as per [semantic versioning spec](https://semver.org/#spec-item-9)
62
63 > Pre-release versions have a lower precedence than the associated normal version. A pre-release version
64 > indicates that the version is unstable and might not satisfy the intended compatibility requirements
65 > as denoted by its associated normal version.
66
67 For example, the following versions (using the [PEP 440](https://peps.python.org/pep-0440/) scheme) are ordered
68 by their precedence and showcase how a release might flow through a development cycle:
69
70 - `1.0.0` is the currently published version
71 - `1.0.1a0` after committing a `fix:` for pre-release
72 - `1.1.0a1` after committing an additional `feat:` for pre-release
73 - `1.1.0b0` after bumping a beta release
74 - `1.1.0rc0` after bumping the release candidate
75 - `1.1.0` next feature release
76
77 ### `--increment-mode`
78
79 #### `--increment-mode=linear` (default)
80
81 Ensures that bumping pre-releases **maintains linearity**.
82
83 Bumping a pre-release with lower precedence than the current pre-release phase maintains the current phase of higher precedence.
84 For example, if the current version is `1.0.0b1` then bumping with `--prerelease alpha` will continue to bump the *beta* phase.
85
86 #### `--increment-mode=exact`
87
88 Applies the exact changes that have been specified with `--increment` or determined from the commit log.
89 For example, `--prerelease beta` will always result in a `b` tag, and `--increment PATCH` will always increase the patch component.
90
91 **Examples**
92
93 The following table illustrates the difference in behavior between the two modes:
94
95 | Increment | Pre-release | Start Version | `--increment-mode=linear` | `--increment-mode=exact` |
96 |-----------|-------------|---------------|---------------------------|--------------------------|
97 | `MAJOR` | | `2.0.0b0` | `2.0.0` | `3.0.0` |
98 | `MINOR` | | `2.0.0b0` | `2.0.0` | `2.1.0` |
99 | `PATCH` | | `2.0.0b0` | `2.0.0` | `2.0.1` |
100 | `MAJOR` | `alpha` | `2.0.0b0` | `3.0.0a0` | `3.0.0a0` |
101 | `MINOR` | `alpha` | `2.0.0b0` | `2.0.0b1` | `2.1.0a0` |
102 | `PATCH` | `alpha` | `2.0.0b0` | `2.0.0b1` | `2.0.1a0` |
103
104 ### `--check-consistency`
105
106 Check whether the versions defined in [version_files][version_files] and the version in Commitizen configuration are consistent before bumping version.
107
108 ```bash
109 cz bump --check-consistency
110 ```
111
112 For example, if we have the following configuration file `pyproject.toml`:
113
114 ```toml title="pyproject.toml"
115 [tool.commitizen]
116 version = "1.21.0"
117 version_files = [
118 "src/__version__.py",
119 "setup.py",
120 ]
121 ```
122
123 and the following version files `src/__version__.py` and `setup.py`:
124
125 ```python title="src/__version__.py"
126 __version__ = "1.21.0"
127 ```
128
129 ```python title="setup.py"
130 from setuptools import setup
131
132 setup(..., version="1.0.5", ...)
133 ```
134
135 When you run `cz bump --check-consistency`, Commitizen will verify that the current version in `pyproject.toml` (`1.21.0`) exists in all files listed in [version_files][version_files].
136 In this example, it will detect that `setup.py` contains `1.0.5` instead of `1.21.0`, causing the bump to fail.
137
138 !!! warning "Partial updates on failure"
139 If the consistency check fails, Commitizen may have already updated some files (like `pyproject.toml` and `src/__version__.py`) before detecting the inconsistency.
140 In this case, you'll need to restore the files to their previous state.
141
142 To resolve this issue:
143
144 1. Restore the modified files to their previous state:
145 ```bash
146 git checkout .
147 ```
148
149 2. Manually update the version in `setup.py` to match the version in `pyproject.toml`:
150 ```diff title="setup.py"
151 from setuptools import setup
152
153 - setup(..., version="1.0.5", ...)
154 + setup(..., version="1.21.0", ...)
155 ```
156
157 3. Run the bump command again:
158 ```bash
159 cz bump --check-consistency
160 ```
161
162 ### `--local-version`
163
164 Bump the local portion of the version.
165
166 For example, if we have the following configuration file `pyproject.toml`:
167
168 ```toml title="pyproject.toml"
169 [tool.commitizen]
170 version = "5.3.5+0.1.0"
171 ```
172
173 When you run `cz bump --local-version`, it will bump only the local version `0.1.0` and keep the public version `5.3.5` intact, bumping to the version `5.3.5+0.2.0`.
174
175 ### `--annotated-tag`
176
177 Create annotated tags.
178
179 It is also available via configuration files.
180
181 For example, in `pyproject.toml`:
182
183 ```toml title="pyproject.toml"
184 [tool.commitizen]
185 annotated_tag = true
186 ```
187
188 !!! note
189 By default, Commitizen uses lightweight tags.
190
191 ### `--annotated-tag-message`
192
193 Create annotated tags with the given message.
194
195 It is also available via configuration files.
196
197 For example, in `pyproject.toml`:
198
199 ```toml title="pyproject.toml"
200 [tool.commitizen]
201 annotated_tag_message = "Annotated tag message"
202 ```
203
204 ### `--changelog-to-stdout`
205
206 Send the incremental changelog generated by `cz bump` to `stdout`.
207 Any other messages generated by `cz bump` will be sent to `stderr`.
208
209 When this flag is used, `--changelog` is implied.
210 However, it is recommended to set `--changelog` (or the setting `update_changelog_on_bump`) explicitly when the option `--changelog-to-stdout` is used.
211
212 !!! note "Useful scenarios"
213 Pipe the newly created changelog to another tool.
214
215 The output can be redirected to an auditing system, or used to create a GitHub Release, etc.
216
217 ```bash
218 cz bump --changelog --changelog-to-stdout > body.md
219 ```
220
221 ### `--git-output-to-stderr`
222
223 Redirects git commands output to `stderr`.
224
225 Useful when used with `--changelog-to-stdout` and piping the output to a file.
226
227 For example, `git commit` output may pollute `stdout`, so it is recommended to use this flag when piping the output to a file.
228
229 ### `--retry`
230
231 If you use tools like [pre-commit](https://pre-commit.com/), you can add this flag.
232 It will retry the commit if it fails the first time.
233
234 Useful to combine with code formatters, like [Prettier](https://prettier.io/).
235
236 ### `--major-version-zero`
237
238 Breaking changes do not bump the major version number.
239
240 Say you have a project with the version `0.1.x` and you commit a breaking change like this:
241
242 ```text
243 fix(magic)!: fully deprecate whatever
244 ```
245
246 and you run
247
248 ```bash
249 cz bump --major-version-zero
250 ```
251
252 Then the version of your project will be bumped to `0.2.0` instead of `1.0.0`.
253
254 !!! note
255 A project in its initial development should have a major version zero,
256 and even breaking changes should not bump that major version from zero. This command ensures that behavior.
257
258 We recommend setting `major_version_zero = true` in your configuration file while a project
259 is in its initial development. Remove that configuration using a breaking-change commit to bump
260 your project's major version to `v1.0.0` once your project has reached maturity.
261
262 !!! warning
263 This option is only compatible with projects that have major version number zero, `0.x.x` for example.
264
265 It fails when used with projects that have a version number greater than zero like `1.x.x`.
266
267 If used together with a manual version, the command also fails.
268
269 ```bash
270 # This fails
271 cz bump 0.1.0 --major-version-zero
272 ```
273
274 ### `--gpg-sign`
275
276 Creates gpg signed tags.
277
278 ```bash
279 cz bump --gpg-sign
280 ```
281
282 !!! note
283 By default, Commitizen uses lightweight tags.
284
285 ### `--template`
286
287 Provides your own changelog jinja template.
288 See [the template customization section](../customization/changelog_template.md)
289
290 ### `--extra`
291
292 Provides your own changelog extra variables by using the `extras` settings or the `--extra/-e` parameter.
293
294 ```bash
295 cz bump --changelog --extra key=value -e short="quoted value"
296 ```
297
298 See [the template customization section](../customization/changelog_template.md).
299
300 ### `--build-metadata`
301
302 Specifies additional metadata in the version string.
303
304 ```bash
305 # creates a version like `1.1.2+yourmetadata`.
306 cz bump --build-metadata yourmetadata
307 ```
308
309 !!! note "Example usage"
310 - Git hash in version
311 - Labeling the version with additional metadata.
312
313 !!! note
314 Commitizen ignores everything after `+` when it bumps the version.
315
316 It is therefore safe to write different build-metadata between versions.
317
318
319 !!! warning
320 Normally, you should not use this functionality, but if you decide to do so, keep in mind that:
321
322 - Version `1.2.3+a`, and `1.2.3+b` are the same version! Tools should not use the string after `+` for version calculation. This is probably not a guarantee (example in helm) even tho it is in the spec.
323 - It might be problematic having the metadata in place when doing upgrades depending on what tool you use.
324
325 !!! warning
326 This parameter is not compatible with `--local-version` as it uses the same part of the version string.
327
328 ### `--get-next`
329
330 Similar to `--dry-run` but only outputs the next version.
331
332 ```bash
333 # outputs 1.0.1 if the current version is 1.0.0 and the increment is PATCH
334 cz bump --get-next
335 ```
336
337 Useful for determining the next version based on CI for non-production environments/builds.
338
339 !!! note "Compare with `--dry-run`"
340 `--dry-run` provides a more detailed output including the changes as they would appear in the changelog file, while `--get-next` only outputs the next version.
341
342 The following is the output of `cz bump --dry-run`:
343
344 ```text
345 bump: version 3.28.0 → 3.29.0
346 tag to create: v3.29.0
347 increment detected: MINOR
348 ```
349
350 The following is the output of `cz bump --get-next`:
351
352 ```text
353 3.29.0
354 ```
355
356 !!! warning
357 The `--get-next` flag will raise a `NoneIncrementExit` if the found commits are not eligible for a version bump.
358
359 For information on how to suppress this exit, see [Ignoring Exit Codes](../exit_codes.md#ignoring-exit-codes).
360
361 ### `--allow-no-commit`
362
363 Allow the project version to be bumped even when there's no eligible version.
364
365 Example usage:
366
367 ```sh
368 # Force to bump a minor version
369 cz bump --increment MINOR --allow-no-commit
370
371 # bump version to 2.0.0 even when there's no breaking changes or even no commits
372 cz bump --allow-no-commit 2.0.0
373 ```
374
375 !!! note "Default increment"
376 The increment is overridden to `PATCH` if there is no increment detected or specified.
377
378 In other words, `cz bump --allow-no-commit` allows you to bump the version to the next patch version even when there is no eligible commit.
379
380 ```sh
381 # will bump to `1.0.1` if the current version is `1.0.0`.
382 cz bump --allow-no-commit
383
384 # bump version to 2.0.0 even when there's no breaking changes or even no commits
385 cz bump --allow-no-commit 2.0.0
386 ```
387
388 !!! note "Behavior with changelog updates"
389 When `update_changelog_on_bump = true` (or `--changelog` is used), `cz bump --allow-no-commit` also generates a changelog entry even if there are no commits in the selected range.
390
391 This makes the new release visible in the changelog while still showing that no commit-based changes were included.
392
393 ### `--tag-format`
394
395 `tag_format` and [version_scheme][version_scheme] are combined to make Git tag names from versions.
396
397 These are used in:
398
399 - `cz bump`: Find previous release tag (exact match) and generate new tag.
400 - Find previous release tags in `cz changelog`.
401 - If `--incremental`: Using the latest version found in the changelog, scan existing Git tags with 89\% similarity match.
402 - `--rev-range` is converted to Git tag names with `tag_format` before searching Git history.
403 - If the `scm` `version_provider` is used, it uses different regexes to find the previous version tags:
404 - If `tag_format` is set to `$version` (default): `VersionProtocol.parser` (allows `v` prefix)
405 - If `tag_format` is set: Custom regex similar to SemVer (not as lenient as PEP440 e.g. on dev-releases)
406
407 Commitizen supports two types of formats, a simple and a more complex.
408
409 ```bash
410 cz bump --tag-format="v$version"
411 ```
412
413 ```bash
414 cz bump --tag-format="v$minor.$major.$patch$prerelease.$devrelease"
415 ```
416
417 In your configuration file:
418
419 ```toml
420 [tool.commitizen]
421 tag_format = "v$major.$minor.$patch$prerelease"
422 ```
423
424 The variables must be preceded by a `$` sign and optionally can be wrapped in `{}`. The default is `$version`.
425
426 Supported variables:
427
428 | Variable | Description |
429 |--------------------------------|---------------------------------------------|
430 | `$version`, `${version}` | fully generated version |
431 | `$major`, `${major}` | MAJOR increment |
432 | `$minor`, `${minor}` | MINOR increment |
433 | `$patch`, `${patch}` | PATCH increment |
434 | `$prerelease`, `${prerelease}` | Prerelease (alpha, beta, release candidate) |
435 | `$devrelease`, `${devrelease}` | Development release |
436
437 ### `--yes`
438
439 Automatically answers “yes” to all interactive prompts during the bump process, allowing the command to run without manual confirmation.
440
441 ```bash
442 cz bump --yes
443 ```
444
445 ### `--version-scheme`
446
447 Format used for the version.
448
449 **Available options**
450
451 - `pep440`: [PEP 440][pep440]: recommended for Python projects, **default** (for legacy reasons)
452 - `semver2`: [Semantic Versioning v2][semver]: recommended for non-Python projects
453 - `semver`: [Semantic Versioning v1](https://semver.org/spec/v1.0.0.html): use if you are stuck with semver v1
454
455 **Examples**
456
457 1. Command line:
458 ```sh
459 cz bump --version-scheme semver2
460 ```
461
462 2. Configuration file:
463 ```toml title="pyproject.toml"
464 [tool.commitizen]
465 version_scheme = "semver2"
466 ```
467
468
469 !!! note
470 [pep440][pep440] and [semver][semver] are quite similar, although their difference lies in
471 how the prereleases look. For example, `0.3.1a0` in pep440 is equivalent to `0.3.1-a0` in semver.
472
473 The following table illustrates the difference between the two schemes:
474
475 | Version Type | pep440 | semver |
476 |--------------|----------------|-----------------|
477 | Non-prerelease | `0.1.0` | `0.1.0` |
478 | Prerelease | `0.3.1a0` | `0.3.1-a0` |
479 | Devrelease | `0.1.1.dev1` | `0.1.1-dev1` |
480 | Dev and pre | `1.0.0a3.dev1` | `1.0.0-a3-dev1` |
481
482
483 !!! note "Incomplete Version Handling"
484 Commitizen treats a three-part version (major.minor.patch) as complete.
485 If your configured version is incomplete (for example, `1` or `1.2`), Commitizen pads missing parts with zeros when it needs `major/minor/patch` for tag formatting.
486 The tag output depends on your `tag_format`: formats using `${version}` keep `1`/`1.2`, while formats using `${major}.${minor}.${patch}` will render `1.0.0`/`1.2.0`.
487
488 When bumping from an incomplete version, Commitizen looks for the latest existing tag that matches the provided release prefix.
489 For example, if the current version is `1.2` and the latest `1.2.x` tag is `1.2.3`, then a patch bump yields `1.2.4` and a minor bump yields `1.3.0`.
490
491 !!! tip
492 To control the behaviour of bumping and version parsing, you may implement your own `version_scheme` by inheriting from `commitizen.version_schemes.BaseVersion` or use an existing plugin package.
493
494
495 ### PEP440 Version Examples
496
497 Commitizen supports the [PEP 440][pep440] version format, which includes several version types. Here are examples of each:
498
499 #### Standard Releases
500
501 ```text
502 0.9.0 # Initial development release
503 0.9.1 # Patch release
504 0.9.2 # Another patch release
505 0.9.10 # Tenth patch release
506 0.9.11 # Eleventh patch release
507 1.0.0 # First stable release
508 1.0.1 # Patch release after stable
509 1.1.0 # Minor feature release
510 2.0.0 # Major version release
511 ```
512
513 #### Pre-releases
514
515 ```text
516 1.0.0a0 # Alpha release 0
517 1.0.0a1 # Alpha release 1
518 1.0.0b0 # Beta release 0
519 1.0.0rc0 # Release candidate 0
520 1.0.0rc1 # Release candidate 1
521 ```
522
523 #### Development Releases
524
525 ```text
526 1.0.0.dev0 # Development release 0
527 1.0.0.dev1 # Development release 1
528 ```
529
530 #### Combined Pre-release and Development
531
532 ```text
533 1.0.0a1.dev0 # Development release 0 of alpha 1
534 1.0.0b2.dev1 # Development release 1 of beta 2
535 ```
536
537 > **Note**: `post` releases (e.g., `1.0.0.post1`) are not currently supported.
538
539 [pep440]: https://www.python.org/dev/peps/pep-0440/
540 [semver]: https://semver.org/
541 [version_files]: ../config/bump.md#version_files