Calculating Git Version

Yuri Goikhman

Yuri Goikhman

Yuri is a DevOps Engineer based in Los Angeles who is responsible for design and implementation of Taboola's ALM process. This provides hundreds of developers an easy and efficient way to develop tens of products and services quickly and safely.

Yuri Goikhman | 26 Dec 2017 | System

Tags: devops, github, tips, web development

Hello Git user. In this blog post I will discuss a technique for a unique version calculation for every Git commit.

You may ask why  we need this, after all every commit in Git is identified by a unique sha1 hash. That’s right, let’s take 2 commits, 4bd92c9 and f5fc029, use their sha1 hash as a version and perform a simple A/B test. The test showed that 4bd92c9 is preferred to f5fc029.

If this is the case, how can we tell:

  • Which version is newer?
  • If 4bd92c9 is included in f5fc029, or vice versa?
  • What branch they were built from?

It seems we need an alternative.

The common standard for the versioning is a SemVer scheme.

We will use its parts as follows:

  • Major – manual increment
  • Minor – every released feature will increment the minor
  • Patch – will always be 0


Now let’s take a look at our Git graph:



We will give a version number for every mentioned commit:

  • M1 – 1.0.0: our init major version
  • M2 – 1.1.0: some new feature released
  • M3 – 1.2.0: feature B released
  • M4 – 1.3.0: feature A released

But what about A1,B1? We need to give them a version number as well in order to identify these builds for various purposes like automation test, deploying on GA environment, etc.  According to the semver rules we need to mark these versions as a next minor pre-release, so we get:

  • A1 – 1.1.0-A-5
  • B1 – 1.2.0-B-3

Let’s put the version numbers in the graph:


Now let’s automate it by using the following logic. If you are on the master branch calculate the version by:


If you are on the feature branch just calculate the version (without putting a tag) by:


Combining all these together we get the following script: