Sub topic 10
Naming versions with Semantic Versioning
This chapter assumes you can create and push tags, and that you know the difference between lightweight and annotated tags. Here you will pick tag names that tell users what changed.
Why tag names need a convention
A tag name is free text. release, final2 and v1.0.0 are all valid. Free text gives users no hint about whether an upgrade is safe. A shared convention fixes that. Semantic Versioning (SemVer) is the most common one.
The MAJOR.MINOR.PATCH format
A SemVer version has three numbers separated by dots. Each number increases when a certain kind of change ships.
| Part | Increase it when | Example |
|---|---|---|
| MAJOR | You make a change that breaks existing users | 1.4.2 to 2.0.0 |
| MINOR | You add a feature that stays compatible | 1.4.2 to 1.5.0 |
| PATCH | You fix a bug without changing behavior otherwise | 1.4.2 to 1.4.3 |
When you raise a number, reset the numbers to its right to zero. Going from 1.4.2 to a new minor release gives 1.5.0, not 1.5.2.
Versions below 1.0.0 (for example 0.3.1) mean the project is still in early development. Anything may change between them.
Pre-release labels
Add a hyphen and a label to mark a version that is not final. Labels sort before the release they lead up to.
1.2.0-alpha.1 < 1.2.0-beta.1 < 1.2.0-rc.1 < 1.2.0Build metadata follows a plus sign, as in 1.2.0+build.5. SemVer ignores it when comparing versions, so two tags that differ only by metadata have the same precedence. Most projects skip it.
Choosing the tag name
SemVer defines the version, 1.2.0. The tag is your own choice. The usual practice is to add a v prefix, so the tag is v1.2.0. This prefix makes tags easy to match with a pattern such as v*, which you will use later in a workflow trigger.
Pick one style and keep it. Mixing 1.2.0 and v1.2.0 breaks sorting and pattern matching.
# Create annotated tags that follow SemVer
git tag -a v1.4.3 -m "Fix crash on empty input"
git tag -a v1.5.0 -m "Add CSV export"
git tag -a v2.0.0 -m "Remove the deprecated v1 API"
# List tags in version order, not alphabetical order
git tag --sort=version:refname -l "v*"Alphabetical order puts v1.10.0 before v1.2.0. The --sort=version:refname option compares the numbers, so v1.2.0 comes before v1.10.0.
Common mistakes
- Reusing a version number for different code. Published tags should not move, so ship a new patch version instead.
- Bumping only the patch number for a breaking change. Users who trust SemVer will upgrade and break.
- Calling a project
1.0.0before its public interface is stable. A 1.0.0 version promises compatibility. - Writing
v1.2orv1.2.0.1. SemVer requires exactly three numbers.
Exercise
The latest tag is v2.3.4. You fix a bug and add one new backward-compatible option in the same release. What should the next tag be, and why?
Show answer
v2.4.0. A new compatible feature raises MINOR, which outranks the bug fix. PATCH resets to 0 when MINOR increases.
Exercise
Why does git tag -l "v*" list v1.10.0 before v1.2.0, and how do you fix the order?
Show answer
Plain listing sorts alphabetically, and the character "1" in "v1.10.0" comes before "2" in "v1.2.0". Use git tag --sort=version:refname -l "v*" to sort by version number.