LAMPlights Personal anecdotes from my experiences using the LAMP stack

5Feb/105

Canonical Version Numbers with Git

Brandon Savage wrote a controversial blog post about why subversion is still more relevant than git.  His main point was that enterprise requires canonical version numbering to track progress in the application and that git cannot do this.   There was a lot of debate about this on Brandson's, but a recent comment by Morgan proved Brandon wrong.

I have never had a problem with the lack of canonical version numbers, but I can see why companies who have used CVS or subversion for a long time would be wary of getting rid of them.  Morgan explains how to use the git describe command to generate canonical version numbers.  I have used git for a while for personal projects and at HauteLook, but have never used this command.  I thought this was a great solution to a problem a lot of companies face when trying to migrate from subversion to git.

Morgan's comment:

git describe –tags –long

This gives you a string like (in the case of one of my projects)

2.1pre5-4-g675eae1

which is formatted as

{last reachable tag name}-{# of commits since that tag}-#{SHA of HEAD}

This gives you a ‘canonical version number’ (spelling corrected) that is monotonically increasing by commits, and unique across multiple repositories of development. If we’re all on the same HEAD, it will return the same value. If we all share the same most-recent-tag, but have different commits, the SHA will be different.

  • http://dejan.lekic.org Dejan Lekic

    I use Git, Subversion, and Bazaar every day almost. I absolutely agree with Bazaar developers who say that VCS system should be user-friendly as well. I seriously see no reason for Git not to have built-in canonical version numbers in the same way Bazaar and Mercurial do, so user can use version number instead of piece of SHA code. After all, version numbers are FOR HUMANS, they are ordered, and if my version is 52345.7.16 I know that previous commit in the branch was 52345.7.15 – I will not need help from Git to tell me what was the previous SHA …

  • http://dejan.lekic.org Dejan Lekic

    A typo in the previous post – I wanted to say that I see no reason for git NOT to have version numbers… I hope this will be fixed by the moderator.

  • Herman Radtke

    Hi Dejan,

    Thanks for the comment. I edited your original post with your correction.

  • eddie

    Thank you for your info.. i created myself a

    cat /usr/local/bin/git-getrevision.sh
    #!/bin/bash
    revisioncount=`git log –pretty=format:” | wc -l`
    projectversion=`git describe –tags –long`

    echo “$projectversion-$revisioncount”

    Then before a commit i do git-getrevision.sh > version

  • eddie

    Ah! nice trick that works just as well.. after creating the above script,

    if you git commit .. while editing your notes in vim, you could press escape
    and :r !git-getrevision.sh .. so your revision number will then be the start of your commit message…

  • Pingback: Using Git to generate an automatic version number