When developing software using several different libraries etc it is always good to know which version of which library is used in a certain installation etc. Though there is no really easy way in Git to solve this as in CVS for instance with its keywords.

Though the following trick creates a nice way of keeping track.

The concept is to utilise GIT Hooks and that we write a script that generates a file with the versioning information in it and then our build system integrates this with the code. The following example is made using C but should be easy to adopt to other languages.

Create and edit the file .git/hooks/post-commit to look like this:

version=$(git describe --tags --long)
echo "#define LIBRARY_NAME_VERSION \""$version"\"" > ./version.h

Make the script executable:

chmod a+x .git/hooks/post-commit

For this to work though you must have made a tag, see GIT Tagging. Once your repository is tagged you can test the script:

more version.h

You should have got something like this:

#define LIBRARY_NAME_VERSION "v0.4-0-g76ed9ef"

Now you can use this in your code for instance to output versioning information at startup.

And finally add version.h to your GIT ignore file or you will have a continuous flow of commits to include the latest change of this file, since it will change after each commit 🙂

echo version.h >> .gitignore