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