Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# If there is no version tag in git this one will be used
VERSION = 0.1.0
# Need to discard STDERR so get path to NULL device
win32 {
NULL_DEVICE = NUL # Windows doesn't have /dev/null but has NUL
} else {
NULL_DEVICE = /dev/null
# Need to call git with manually specified paths to repository
BASE_GIT_COMMAND = git --git-dir $$PWD/.git --work-tree $$PWD
# Trying to get version from git tag / revision
GIT_VERSION = $$system($$BASE_GIT_COMMAND describe --always --tags 2> $$NULL_DEVICE)
# Check if we only have hash without version number
!contains(GIT_VERSION,\d+\.\d+\.\d+) {
# If there is nothing we simply use version defined manually
isEmpty(GIT_VERSION) {
} else { # otherwise construct proper git describe string
GIT_COMMIT_COUNT = $$system($$BASE_GIT_COMMAND rev-list HEAD --count 2> $$NULL_DEVICE)
# Turns describe output like 0.1.5-42-g652c397 into ""
GIT_VERSION ~= s/-/"."
GIT_VERSION ~= s/g/""
# Now we are ready to pass parsed version to Qt
win32 { # On windows version can only be numerical so remove commit hash
VERSION ~= s/\.\d+\.[a-f0-9]{6,}//
# Adding C preprocessor #DEFINE so we can use it in C++ code
# also here we want full version on every system so using GIT_VERSION
# By default Qt only uses major and minor version for Info.plist on Mac.
# This will rewrite Info.plist with full version
macx {
INFO_PLIST_PATH = $$shell_quote($${OUT_PWD}/$${TARGET}.app/Contents/Info.plist)
QMAKE_POST_LINK += /usr/libexec/PlistBuddy -c \"Set :CFBundleShortVersionString $${VERSION}\" $${INFO_PLIST_PATH}
Copy link

julien1619 commented May 29, 2014

I tried your pri file but I had to edit some lines to get it work and I don't know why.

First, with line 12, qmake tried to execute the git command from inside the .git folder. In order to get it work, line 12 becomes:


Another weird thing, contains and isEmpty functions didn't work as expected and I had to replace GIT_VERSION by "$${GIT_VERSION}" inside these functions.

Finally, in the RC file for Windows, version number has 4 digital parts, so removing the commit number from the VERSION string is optional.

Thank you for your script, it saved me a lot of time!

Copy link

netnazgul commented Dec 2, 2014

Adding to previous comment - qmake doesn't like the line

!contains(GIT_VERSION,\d+\.\d+\.\d+) {

where it says that unescaped backslashes are deprecated, meaning that regexp doesn't work after it.
It doesn't like win32 version fix either, but leaving commit count as 4th number and dot without backslash fixes the problem (so "." works the same as ".").

Copy link

z00m1n commented Apr 12, 2015

Thanks for your effort, this is a great start to getting version info from git into my application! :-)

Out of the box, the ante-last step seems to fail on Mac OS X 10.8 / XCode Version 5.1.1 (5B1008) with

Set: Entry, ":CFBundleShortVersionString", Does Not Exist

Has this entry possible been introduced only in a later version ? What Mac OS X version was this written on ?

Copy link

z00m1n commented Apr 12, 2015

It seems, instead of CFBundleShortVersionString, I can also use CFBundleGetInfoString; this is set to "Created by Qt/QMake" by default and is displayed as the contents of the 'Version' field in the Mac OS X info dialog (the one displayed by right-clicking the .app application file and selecting 'Get Info').

Copy link

asmaloney commented Sep 2, 2016

I realize this is a couple of years old, but if anyone else lands here this might be useful.

You can replace the whole !contains(GIT_VERSION,\d+\.\d+\.\d+) { section that is recreating the tag-num-ghash format if you simply change the initial git command from:

git describe --always --tags


git describe --long

This will result in git describe always returning it in the tag-num-ghash format. (Reference: the git docs)

Copy link

fallomermo commented Mar 4, 2018

Qual o melhor script?

Copy link

mrbindraw commented Apr 26, 2021

Please, check my repo. Set Qt App version from git.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment