Get a Version Name for a Commit

In some of the projects I work on, we publish an artifact for every build on the main branch. We don’t want to give these nice semver names like 1.5.0 because that’s too much work! Instead we just wanna generate a version name based on the most-recent commit that it includes.

Here’s a Bash one-liner:

VERSION=$(TZ=UTC0 git log -1 --date=local --pretty="format:%cd.%h" --date="format-local:%Y%m%d.%H%M%S")

Given a Git commit like this:

$ git log
commit 6018f4a376148525bd357f3d7464da90123ef1d7 (HEAD)
Author: Jesse Wilson <>
Date:   Mon Jun 24 18:11:35 2024 -0400

    Do not add pineapples unless requested

It’ll create version string like this:

$ echo $VERSION

It’s an 8-digit date (2024-June-24) followed by a 6-digit time (22:11:35) followed by a short hash of the commit.

Here’s why this format is good:

  • It’s human-readable. I’ve seen seconds-since-epoch timestamps for versions and find these much easier to work with: ‘That artifact is from last month; we should update it.’
  • It uses the timestamp of the commit. We used to run date -u '+%Y%m%d.%H%M%S' but that returns the timestamp that the version was requested, not the timestamp of the commit. If our builds run out-of-order, that’s a problem.
  • It doesn’t care about the time zone of the machine producing it. I run my Mac in America/Toronto but the version timestamp is UTC.
  • It includes a Git hash. I’ve found it occasionally quite handy to look up the exact commit for a project dependency.

If you’re generating version names from git commits, please use this!