GitVersion Documentation
repository·main·Indexed 25 days ago
https://github.com/gittools/gitversionGitVersion automates Semantic Versioning (SemVer) by analyzing Git history to determine the correct version for the current commit. Designed for CI/CD pipelines, it supports Windows, Linux, and macOS. The tool is available via NuGet, Chocolatey, Homebrew, Winget, Docker, Azure Pipelines, and GitHub Actions. Documentation covers branching strategies like GitFlow, configuration migration for v6.0.0 and v4.0.0, and runtime requirements for .NET 10.
What's inside GitVersion
- GitVersion is a tool that automates Semantic Versioning (SemVer) by analyzing your Git history. It determines the correct version of the commit currently being built, allowing for automated versioning in CI/CD pipelines. It supports Windows, Linux, and macOS.
Understand the GitVersion version calculation process
mainGitVersion uses a two-step process to determine the version number:
- Calculate the base version: It identifies the highest version number found across all available version sources.
- Calculate the next version: It uses the base version and increments it based on your branch configuration and then appends build metadata (the part after the
+).
If the current commit is explicitly tagged, GitVersion uses that tag as the version immediately.
Understand the advantages of using GitVersion
mainGitVersion automates Semantic Versioning (SemVer) by analyzing your git history rather than relying on manual version files or build server configurations.
Key benefits include:
- Deterministic Versioning: Rebuilding tags always produces the same version.
- No Manual Increments: You do not need to rebuild or commit just to increment a version number.
- Single Source of Truth: Eliminates the need to duplicate version information in multiple places (like
version.txtor branch names). - Branch-Aware SemVer: Each branch calculates its own SemVer, and versions flow correctly between branches during merges.
- Unique PR Versions: Pull requests automatically produce unique pre-release version numbers.
- Automated Integration: Handles NuGet SemVer issues, build server integration, and assembly info updates automatically.
How GitVersion applies SemVer to Git workflows
mainGitVersion automates the calculation of the next semantic version based on your Git workflow. The version increment logic varies depending on the branching model used:
- GitFlow: When the
mainbranch is tagged, GitVersion will typically bump the minor version on thedevelopbranch. - GitHubFlow: GitVersion will typically bump the patch version.
GitVersion provides various versioning formats via Variables. For example, while standard SemVer follows
{major}.{minor}.{patch}-{tag}, theFullSemVervariable includes build metadata:{major}.{minor}.{patch}-{tag}+{buildmetadata}.- GitFlow: When the
Understand GitVersion configuration defaults
mainStarting from version 3.0, GitVersion uses a configuration-driven approach rather than hard-coded branching strategies.
- Default Strategies: GitVersion ships with internal default configurations that support
GitHubFlow,GitFlow, and potentially others. - Develop Branch Behavior: The
developbranch is set toContinuousDeploymentmode by default to align with standard GitFlow usage.
- Default Strategies: GitVersion ships with internal default configurations that support
Understand the GitVersion v3 version calculation process
mainGitVersion v3 calculates versions using a three-step process driven by configuration rather than hard-coded branch logic. The steps are:
- Tag Check: If the current commit is tagged, GitVersion uses that tag and adds build metadata (excluding commit count). This bypasses the subsequent two steps.
- Strategy Evaluation: A set of versioning strategies are evaluated to determine the base version and associated metadata.
- Selection: The highest base version identified from the strategies is selected, and the final version is calculated based on that base.
Understand Continuous Deployment mode in GitFlow
mainWhen using GitFlow, builds off the
developbranch can be configured to increment on every commit. This is known as continuous deployment mode.By default,
developbuilds are assigned analphapre-release tag to ensure they are sorted higher than release branches. If you need to consume these packages, it is recommended to publish them to a separate NuGet feed (an alpha channel) to avoid polluting stable feeds.Configure GitVersion as a Command Line build step in TeamCity
mainTo use GitVersion in TeamCity via a standard command line step, create a new build step with the following configuration:
- Runner type:
Command Line - Run:
Executable with parameters - Command executable:
GitVersion.exe - Command parameters:
--output buildserver --update-assembly-info true
GitVersion will automatically write system parameters into TeamCity, making them available as build parameters (e.g.,
GitVersion.Major) for subsequent build steps.- Runner type:
Write GitVersion variables to a Dotenv file
mainTo make GitVersion variables available in your environment or to pass them between CI steps (e.g., in GitHub Actions or GitLab CI), redirect the
dotenvoutput to a file. You can then load this file into your environment.# Write version variables in Dotenv format into a file gitversion --output dotenv > gitversion.envUnderstand GitHub Flow branching strategy
mainGitHub Flow is a simple branching strategy suitable for continuous delivery. It is commonly used by open source projects and follows these principles:
- Mainline development: Performed on the
mainbranch. - Feature branches: Work is done on separate branches and merged into
mainvia pull requests. - Limitations: It does not provide a mechanism to manage or maintain old releases and only allows working on a single release at a time.
- Mainline development: Performed on the
Workaround Octopus Deploy versioning conflicts with Tagging
mainIf using GitHubFlow, you can avoid versioning conflicts by tagging stable versions manually:
- Set GitVersion to
continuous-deploymentmode (somainproduces-ci.xpre-release builds). - Configure CI builds to only create NuGet packages for stable builds.
- Tag
mainwith a stable version and push the tag. - The CI build triggers, and GitVersion respects the tag to produce a stable version.
- The stable package is pushed to Octopus.
- The next build will automatically increment and produce pre-release packages for the subsequent version.
- Set GitVersion to
Integrate GitVersion with GitLab CI
mainTo use GitVersion within GitLab CI, you have two primary options:
- Use the MSBuild Task.
- Ensure the GitVersion executable is included in your runner's
PATH.