brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: git describe --tags names a commit after the nearest tag

Git
~/tips/tip-git-describe-tags-version-name

git describe --tags

Gives a commit a readable name made of the closest tag and the number of commits since it.

Version numbers that come from a tag are nicer than a bare hash. git describe builds them from the history.

git describe --tags
git describe --tags --always

On a commit that is tagged, you just get the tag name. On later commits you get a string like v1.4-3-g1a2b3c4: the nearest tag, the number of commits since that tag, and a g followed by the abbreviated hash. The documentation describes it as somewhat human-readable and stable, which makes it useful as a build version in scripts.

Without --tags, git only uses annotated tags. Adding it also allows lightweight tags, which are the kind git tag v1.4 creates without a message.

--always shows the abbreviated hash when no tag can be found, so the command never fails in a script:

VERSION=$(git describe --tags --always)

The caveat is that a repository without any matching tag gives the error that no names were found. That also happens in a shallow clone, as used by many CI systems, because the tags are not there. Fetch the tags first, or use --always to get a hash instead of an error.

Further reading: git-describe documentation

More Git tips git show-branch main feature git log --stat -5 git merge --no-ff feature -m "Merge feature" git merge --squash feature git merge --abort All Git tips (144) All topics

Link