TExcelTAD
Home › Release Engineering & CI/CD › git describe & Version Strings

git describe & Version Strings

Between two releases, every build still needs a version: a nightly, a pull-request build, a binary a colleague asks you to try. Inventing one by hand is how "1.4.2" ends up meaning three different binaries. git describe derives a unique, sortable-enough name for any commit from the nearest release tag, and most version-stamping schemes are built on it.

Reading the output

v1.4.2-3-g1a2b3c4-dirty
│      │  │       └── the working tree has uncommitted changes (--dirty)
│      │  └────────── "g" + abbreviated commit id (g = git)
│      └───────────── 3 commits since the tag
└──────────────────── nearest annotated tag reachable from HEAD

On the tagged commit itself, the output is just the tag: v1.4.2. That property is what makes it useful: release builds and in-between builds come out of the same command.

bash · try it yourself
mkdir app && cd app && git init -q
c() { echo "$1" >> app.txt; git add app.txt; git commit -q -m "$1"; }
c "feat: first"; git tag -a v1.4.2 -m "1.4.2"
echo "on the tag:        $(git describe)"
c "fix: a"; c "feat: b"; c "docs: c"
echo "3 commits later:   $(git describe)"
echo "always long form:  $(git describe --long)"
echo "last tag only:     $(git describe --abbrev=0)"
echo "local edit" >> app.txt
echo "uncommitted edit:  $(git describe --dirty)"
git checkout -q -- app.txt
git tag -a nightly-2026-01-15 -m "nightly"
echo "nearest any tag:   $(git describe)"
echo "release tags only: $(git describe --match 'v[0-9]*')"
on the tag:        v1.4.2
3 commits later:   v1.4.2-3-g0da980b
always long form:  v1.4.2-3-g0da980b
last tag only:     v1.4.2
uncommitted edit:  v1.4.2-3-g0da980b-dirty
nearest any tag:   nightly-2026-01-15
release tags only: v1.4.2-3-g0da980b

The last two lines show a real hazard: once a non-release tag (nightly-…) appears, plain git describe happily uses it. Release scripts should always say which tags count with --match 'v[0-9]*' (and --exclude for exceptions).

Options a release script uses

Option Effect
--tags also consider lightweight tags (see Tags)
--match 'v[0-9]*' / --exclude '*-rc*' only/never consider tags matching the glob
--abbrev=0 print only the tag name: "the last release"
--long always print tag-N-gHASH, even on the tag (-0-g…)
--dirty[=-modified] append a suffix if tracked files have local changes
--always fall back to the abbreviated commit id when no tag is reachable (fresh repos, shallow clones)
--first-parent follow only the first parent of merges, so tags on merged-in release branches don't count

CI gotcha: shallow clones

CI systems check out a single commit by default (actions/checkout uses fetch-depth: 1). With no history and no tags, git describe fails:

bash · try it yourself
git init -q --bare remote.git
git clone -q remote.git dev 2>/dev/null && cd dev
for i in 1 2 3; do echo $i > f; git add f; git commit -q -m "fix: change $i"; done
git tag -a v2.0.0 HEAD~1 -m "2.0.0"
git push -q origin main v2.0.0
cd ..
git clone -q --depth 1 file://$PWD/remote.git shallow 2>/dev/null && cd shallow
git describe 2>&1
echo "fallback: $(git describe --always)"
git fetch -q --unshallow --tags
echo "after unshallow: $(git describe)"
fatal: No names found, cannot describe anything.
fallback: 9cfea10
after unshallow: v2.0.0-1-g9cfea10

In GitHub Actions the fix is fetch-depth: 0 on the checkout step (all history and tags); Workflows & Triggers shows it in context. Every tool in this course that looks for "the previous release" (semantic-release, git-cliff, changesets) needs the full history for the same reason.

From describe output to a version

v1.4.2-3-g1a2b3c4 is unique but not SemVer, and it sorts badly. A common convention turns it into a pre-release of the next patch with the commit as build metadata, so dev builds sort after the last release and before the next one:

bash · try it yourself
mkdir app && cd app && git init -q
c() { echo "$1" >> app.txt; git add app.txt; git commit -q -m "$1"; }
version() {
  local d; d=$(git describe --match 'v[0-9]*' --long --dirty)
  if [[ $d =~ ^v([0-9]+)\.([0-9]+)\.([0-9]+)-([0-9]+)-g([0-9a-f]+)(-dirty)?$ ]]; then
    local ma=${BASH_REMATCH[1]} mi=${BASH_REMATCH[2]} pa=${BASH_REMATCH[3]} n=${BASH_REMATCH[4]} sha=${BASH_REMATCH[5]}
    if [[ $n == 0 && -z ${BASH_REMATCH[6]} ]]; then
      echo "$ma.$mi.$pa"
    else
      echo "$ma.$mi.$((pa + 1))-dev.$n+g$sha${BASH_REMATCH[6]:+.dirty}"
    fi
  else
    echo "unparseable: $d" >&2; return 1
  fi
}
c "feat: first"; git tag -a v1.4.2 -m "1.4.2"
echo "release build: $(version)"
c "fix: a"; c "fix: b"
echo "dev build:     $(version)"
echo x >> app.txt
echo "local build:   $(version)"
release build: 1.4.2
dev build:     1.4.3-dev.2+gd88c987
local build:   1.4.3-dev.2+gd88c987.dirty

The same idea is built into several ecosystems, so you rarely write this function for real: Go stamps VCS information into binaries automatically (Version Stamping & Build Info), Python's setuptools-scm and hatch-vcs derive versions from tags, and Cargo/CMake projects pass git describe output in through a build script or configure_file. Whatever produces it, the rule is the same: the tag is the single source of truth, and the version inside the binary is derived from it, never typed twice.

Test yourself

What does v2.1.0-5-g9f8e7d6 say?

  1. Version 2.1.0, build 5
  2. The commit 9f8e7d6 is 5 commits after the tag v2.1.0
  3. Five files changed since v2.1.0
  4. The 5th pre-release of 2.1.0

git describe fails in CI with "No names found", but works locally. Most likely cause?

  1. Git is too old on CI
  2. CI made a shallow clone without tags; fetch full history (fetch-depth: 0)
  3. The tag is lightweight
  4. The working tree is dirty

Why add --match 'v[0-9]*' to git describe in a release script?

  1. It is faster
  2. So other tags (nightly, deploy markers) can't be picked as the base version
  3. It enables lightweight tags
  4. It is required with --long