Skip to content

[ISSUE]: Mainline: version on main falls back to 0.0.x when a branch named in a merge message still exists #5405

Open

Description

@janbernloehr

Prerequisites

  • I have written a descriptive issue title
  • I have searched issues to ensure it has not already been reported

GitVersion package

GitVersion.Tool, GitVersion.MsBuild

GitVersion version

6.8.2

Operating system

Windows

What are you seeing?

With the Mainline strategy, the version calculated on main depends on
whether a branch that is only named in a merge message on main still
exists. If it exists, the tag on main is ignored and the version is counted
from 0.0.0. If the branch is deleted, the same commit gets the expected
version.

In the minimal repro below, the same commit on main gets:

  • 1.0.0 while branch part1 exists. The base version is 0.0.2-1, and the
    result looks like it is then raised to the tag.
  • 1.0.7 after git branch -D part1.

The history is the usual "update a stacked feature branch, then complete the
pull request" flow:

  1. part2 is branched off part1.
  2. main is merged into part1.
  3. part1 is merged into part2 ("Merge branch 'part1' into part2").
  4. part2 is merged into main.

The merge message names part1. The log shows that GitVersion then looks for
the source of part1 and takes the merge base with main to be Part 1, not
the tip of part1. It ends up with CommitOnTrunk: Force version increment '0.0.2-1'.

In our real repository (Azure DevOps, PR merges with "Merged PR n: …"
messages, completed branches kept), every build on master produced
11.1.0 instead of 11.1.3xx after upgrading from GitVersion 5.12 to 6.8.2.
That was caused by one such kept branch. There is no warning and the build
succeeds, so it publishes packages with a wrong, lower version. On CI this
is easy to hit, because the checkout fetches all remote branches and
GitVersion creates local branches for them.

What is expected?

The version on main should not depend on whether a branch named in a
merge message still exists. The tag 1.0.0 on main should always be the
version source, so the result is the same with and without part1.

Workaround: setting track-merge-message: false on the main branch gives
the same version (1.0.9 in the repro) whether or not part1 exists.

Steps to Reproduce

Run this script with GitVersion.Tool 6.8.2 (dotnet tool install -g GitVersion.Tool --version 6.8.2). It works in bash on Linux and Git Bash on
Windows; set GV to the path of dotnet-gitversion if it is not on PATH.

#!/usr/bin/env bash
# GitVersion 6.8.2, Mainline strategy: the version on main changes when a
# branch named in a merge message on main still exists.
set -e
GV=${GV:-dotnet-gitversion}
rm -rf repro && mkdir repro && cd repro
git init -q -b main
git config user.name test; git config user.email test@example.com
cat > GitVersion.yml <<'YML'
workflow: GitFlow/v1
strategies:
- Mainline
branches:
  main:
    mode: ContinuousDeployment
    increment: Patch
YML
git add GitVersion.yml; git commit -q -m "Initial commit"
git tag 1.0.0
pr() { # a short-lived branch with one commit, merged into main with --no-ff
  git checkout -q -b "pr$1" main; git commit -q --allow-empty -m "Change $1"
  git checkout -q main; git merge -q --no-ff "pr$1" -m "Merged PR $1"; git branch -q -D "pr$1"; }

pr 1
git checkout -q -b part1 main;  git commit -q --allow-empty -m "Part 1"
git checkout -q -b part2 part1; git commit -q --allow-empty -m "Part 2"   # part2 builds on part1
git checkout -q main; pr 2
git checkout -q part1; git merge -q --no-ff main  -m "Merge branch 'main' into part1"
git checkout -q part2; git merge -q --no-ff part1 -m "Merge branch 'part1' into part2"
git checkout -q main;  git merge -q --no-ff part2 -m "Merged PR 3"; git branch -q -D part2
pr 4

ver() { "$GV" "$(pwd -W 2>/dev/null || pwd)" /nocache | grep '"FullSemVer"' | tr -d ' ",'; }
echo "branch part1 exists:  $(ver)"
git branch -q -D part1
echo "branch part1 deleted: $(ver)"

Output:

branch part1 exists:  FullSemVer:1.0.0
branch part1 deleted: FullSemVer:1.0.7

Resulting history:

*   Merged PR 4 (HEAD -> main)
|\
| * Change 4
|/
*   Merged PR 3
|\
| *   Merge branch 'part1' into part2
| |\
| | *   Merge branch 'main' into part1 (part1)
| | |\
| |_|/
|/| |
* | |   Merged PR 2
|\ \ \
| * | | Change 2
|/ / /
| * / Part 2
| |/
| * Part 1
|/
*   Merged PR 1
|\
| * Change 1
|/
* Initial commit (tag: 1.0.0)

Each step is needed. The bug does not show when:

  • the message of the part1 → part2 merge does not name part1,
  • main is not merged into part1 first, or
  • the branch named in the message is deleted.

RepositoryFixture Test

No response

Output log or link to your CI build (if appropriate).

Mainline part of the log with branch `part1` present (`/verbosity Diagnostic`,
DEBUG lines removed):


INFO Running against branch: main ('0e121dd' - Merged PR 4)
INFO -< Begin: Fetching the base versions for version calculation... >-
  INFO -< Begin: Calculating base versions for 'main' >-
    INFO -< Begin: [Using 'MainlineVersionStrategy' strategy] >-
      INFO -< Begin: Getting tagged semantic versions on branch 'refs/heads/main'. TagPrefix: [vV]? and Format: Strict >-
      INFO -< End: Getting tagged semantic versions on branch 'refs/heads/main'. TagPrefix: [vV]? and Format: Strict (Took: 8.39ms) >-
      INFO -< Begin: Finding branches source of 'main' >-
      INFO -< End: Finding branches source of 'main' (Took: 17.55ms) >-
      INFO -< Begin: Finding branches source of 'part1' >-
        INFO -< Begin: Finding merge base between 'part1' and 'main'. >-
          INFO Found merge base of ''12eaa6b' - Merge branch 'main' into part1'
          INFO next merge base --> '13c2268' - Part 1
          INFO Merge base of 'part1' and 'main' is ''13c2268' - Part 1'
        INFO -< End: Finding merge base between 'part1' and 'main'. (Took: 18.85ms) >-
      INFO -< End: Finding branches source of 'part1' (Took: 26.90ms) >-
      INFO CommitOnTrunk: Force version increment '0.0.2-1' +semver 'Patch' with label '' based on commit '723c890'.
    INFO -< End: [Using 'MainlineVersionStrategy' strategy] (Took: 317.22ms) >-
  INFO -< End: Calculating base versions for 'main' (Took: 336.23ms) >-
INFO -< End: Fetching the base versions for version calculation... (Took: 344.75ms) >-
INFO -------------------------------------------------------
INFO Base version used: CommitOnTrunk: Take '0.0.2-1' based on commit '723c890'.


Possibly related: thomhurst/TUnit#7215 reports an `InvalidOperationException`
in the Mainline strategy (GitVersion 6.7.0) when a merge message names
another branch that exists on origin.

Activity

  1. arturcic commented on Oct 23, 2026

    @arturcic
    Member

    Codex here, responding on behalf of @arturcic.

    Thanks for the detailed reproduction, @janbernloehr. We confirmed it on both Git backends. #5295 fixes the branch-presence dependency and now includes a regression asserting 1.0.8 with part1 present, deleted or recreated. The corrected traversal counts Part 1 once in the receiving history, explaining the additional patch over 1.0.7. The original 1.0.0 tag remains the baseline. The issue will close when #5295 merges.

  2. modified the milestones: 7.0.x, 7.1.x on Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions