By admin , 29 April 2026

Synopsis

git describe --tags --dirty --always [--match 'v[0-9]*']

Description

This page covers git describe from a versioning angle: how to design a tag scheme that drives reproducible build version strings. The output of git describe is widely used by build systems (Make, CMake, Cargo, npm scripts) to generate a version string that includes a base tag plus distance and SHA.

The recommended pattern: use annotated semver tags like v1.2.3; build scripts call git describe --tags --dirty --always --match 'v[0-9]*'; the resulting string is embedded into binaries via a generated header or constant. This produces deterministic versions that reflect both the latest release and any unreleased work since.

In day-to-day use, git describe (versioning) integrates closely with shell aliases, editor plugins, and continuous integration. Power users often add aliases that combine flags they always pass, or wrap the command in scripts that enforce team conventions. Output formatting can be customized via Git config — pretty formats, color schemes, and pager behavior are all tunable. When something goes wrong, the first diagnostic step is usually to re-run the command with GIT_TRACE=1 in the environment, which reveals the underlying plumbing calls. For unusual situations, the --help output (git describe (versioning) --help) opens the full manual page with details on every option, including those rarely used in casual workflows but essential for debugging or scripting at scale.

Understanding how git describe (versioning) interacts with the rest of Git's data model — the object database, the index, refs, and the working tree — pays dividends. Each command operates on some subset of these pieces, and knowing which it touches helps predict outcomes and recover from mistakes. Reading the official Git documentation alongside hands-on practice in a throwaway repository is the fastest way to internalize the nuances. Most production issues with Git stem from one of three causes: surprising default behavior, partial network operations, or rewriting history that was already shared. A working mental model of git describe (versioning)'s side effects helps avoid all three.

Common Options

OptionDescription
--tagsUse lightweight as well as annotated tags.
--dirty[=<mark>]Append a marker if the tree is dirty.
--alwaysFall back to abbreviated SHA when no tag is reachable.
--longForce long format even at a tag.
--match <pattern>Restrict to tags matching a pattern (e.g. exclude nightly-*).
--exclude <pattern>Exclude matching tags.
--abbrev=<n>Abbrev SHA length.

Examples

# Generate a version string for a build
VERSION=$(git describe --tags --dirty --always --match 'v[0-9]*')
echo "${VERSION#v}" > VERSION

# In a Makefile:
VERSION := $(shell git describe --tags --dirty --always)
CFLAGS += -DVERSION="\"$(VERSION)\""

# In package.json prepublish script:
node -e "p=require('./package.json'); p.version=process.env.VERSION; require('fs').writeFileSync('package.json', JSON.stringify(p,null,2))"

Common Mistakes

Using lightweight tags breaks git describe without --tags. Including release candidate or nightly tags in your match pattern produces noisy versions for stable builds. Shallow CI clones may have no tags at all — fetch them explicitly: git fetch --tags --unshallow.

Related Commands

git tag, git rev-parse, git for-each-ref, git log