Source code control has had various eras - there was the CVS era, followed by the SubVersion era, followed by the distributed source control systems era - Mercurial and Git.

Git (currently) has won that battle, and is by far the most used source control versioning system.

In the course if your work, perhaps in the build process, or just as a FYI, you might want to know some information about the repository you are in.

For example, if I needed to know the current tag of the branch I am in …

git describe --tags

This will return the following (assuming there was an actual tag)

1.1.46-10-g4c46273

Suppose, as well, we needed to get the current branch.

This requires a bit more work.

If you are using Bash:

echo $(git rev-parse --abbrev-ref HEAD)

This returns the following (depending on your repository):

master

If you are using PowerShell:

$branch = git rev-parse --abbrev-ref HEAD

You can also use a variable to capture this.

In Bash:

branch=$(git rev-parse --abbrev-ref HEAD)
echo $branch

In PowerShell

$branch = git rev-parse --abbrev-ref HEAD
Write-Host $branch

While workable, it can quickly become cumbersome stitching together various scripts to get for you your repository information, especially if you are using it in a context such as continuous integration or automated release management.

There is a command line utility that can help with this – Gitversion.

It is a .NET tool and can be installed in several ways depending on your operating system.

Once installed you can run it as follows:

dotnet gitversion

This will return the following (depending on your repository!):

{
  "AssemblySemFileVer": "1.1.47.0",
  "AssemblySemVer": "1.1.47.0",
  "BranchName": "master",
  "BuildMetaData": null,
  "CommitDate": "2025-04-11",
  "CommitsSinceVersionSource": 11,
  "EscapedBranchName": "master",
  "FullBuildMetaData": "Branch.master.Sha.d8cb74eef5c84a5133bf14c19fedfc0f4d0a181f",
  "FullSemVer": "1.1.47-11",
  "InformationalVersion": "1.1.47-11+Branch.master.Sha.d8cb74eef5c84a5133bf14c19fedfc0f4d0a181f",
  "Major": 1,
  "MajorMinorPatch": "1.1.47",
  "Minor": 1,
  "Patch": 47,
  "PreReleaseLabel": "",
  "PreReleaseLabelWithDash": "",
  "PreReleaseNumber": 11,
  "PreReleaseTag": "11",
  "PreReleaseTagWithDash": "-11",
  "SemVer": "1.1.47-11",
  "Sha": "d8cb74eef5c84a5133bf14c19fedfc0f4d0a181f",
  "ShortSha": "d8cb74e",
  "UncommittedChanges": 0,
  "VersionSourceSha": "47a095e8d61509938a7b43ea8d3497b5b8a7d043",
  "WeightedPreReleaseNumber": 55011
}

Given it is in Json, we have lots of options how to pick the data.

On Bash or PowerShell, you can pipe the output to the jq tool and extract whichever property you are interested in:

dotnet gitversion | jq .BranchName

This, will print the following:

"master"

You can even extract multiple values

dotnet gitversion | jq -r '.BranchName, .UncommittedChanges, .WeightedPreReleaseNumber'

You will get back a list of values:

master
0
55011

If you are using PowerShell, you can do it like this:

// Convert the json to an object
$json = dotnet gitversion | ConvertFrom-json 
// Get the properties
Write-Host $json.BrancName

In the context of a build environment like GitHub or TeamCity, you can output the data in DotEnv format, using the switch /output dotenv

dotnet gitversion /output dotenv

The output will look like this:

GitVersion_AssemblySemFileVer='1.1.47.0'
GitVersion_AssemblySemVer='1.1.47.0'
GitVersion_BranchName='master'
GitVersion_BuildMetaData=''
GitVersion_CommitDate='2025-04-11'
GitVersion_CommitsSinceVersionSource='11'
GitVersion_EscapedBranchName='master'
GitVersion_FullBuildMetaData='Branch.master.Sha.d8cb74eef5c84a5133bf14c19fedfc0f4d0a181f'
GitVersion_FullSemVer='1.1.47-11'
GitVersion_InformationalVersion='1.1.47-11+Branch.master.Sha.d8cb74eef5c84a5133bf14c19fedfc0f4d0a181f'
GitVersion_Major='1'
GitVersion_MajorMinorPatch='1.1.47'
GitVersion_Minor='1'
GitVersion_Patch='47'
GitVersion_PreReleaseLabel=''
GitVersion_PreReleaseLabelWithDash=''
GitVersion_PreReleaseNumber='11'
GitVersion_PreReleaseTag='11'
GitVersion_PreReleaseTagWithDash='-11'
GitVersion_SemVer='1.1.47-11'
GitVersion_Sha='d8cb74eef5c84a5133bf14c19fedfc0f4d0a181f'
GitVersion_ShortSha='d8cb74e'
GitVersion_UncommittedChanges='0'
GitVersion_VersionSourceSha='47a095e8d61509938a7b43ea8d3497b5b8a7d043'
GitVersion_WeightedPreReleaseNumber='55011'

These can be used to set environment variables, or output to a .env file.

Given that the values come back with each on it’s own line, you can use your favourite grepping tool, so ripgrep or grep.

Using ripgrep:

dotnet gitversion /output dotenv | rg SemVer

The following will be the result.

GitVersionGrep

To get all the values into a .env file, you can run the folowing command as part of your build process.

gitversion /output dotenv > gitversion.env

TLDR

GitVersion is a dotnet tool that makes it easy to extract git information for use in a build process.

Happy hacking!