How can I get the Git build number and embed it in a file?

To get Git revision in php, I use something like this (change your paths to .Git catalog)

public static function getGitRevision()
     {
         $rev = trim(file_get_contents(NT_Path::ROOT . "/.git/HEAD"));

         if (substr($rev, 0, 4) == 'ref:') {
             $ref =  end(explode('/', $rev));
             $rev = trim(file_get_contents(NT_Path::ROOT . "/.git/refs/heads/{$ref}"));
         }

         return $rev;
     }

Here's what I do:

As part of my build process I run the following script (paraphrased, since I'm not at Xcode right now)

git describe --all > version.txt

Inside my application I read the version number out of this file and display it to the user (when necessary). Make sure you add version.txt to your .gitignore. The benefit of doing it this way is that if you tag your releases git describe will just output the tag, otherwise it'll output the commit hash.


For me, git describe didn't initially give the hashtag. The following did, however:

git describe --all --long

This results in something of the by kubi described format. Supposing you would only want the last part (hashtag) something like the following would do (saving to version.txt file):

git describe --all --long | tr "-" " " | awk '{ print $3 }' > version.txt

EDIT: As a friend pointed out to me this can actually be done using just cut instead, if you so desire:

git describe --all --long | cut -d "-" -f 3 > version.txt

Note: it is interesting to see how Git itself computes its own build number.
That just evolved in Git 2.12 (Q1 2017)

See commit a765974 (04 Dec 2016) by Ramsay Jones (``).
Helped-by: Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit 0a45050, 19 Dec 2016)

GIT-VERSION-GEN 

VN=$(git describe --match "v[0-9]*" HEAD 2>/dev/null) 
# instead of
VN=$(git describe --match "v[0-9]*" --abbrev=7 HEAD 2>/dev/null)

GIT-VERSION-GEN: do not force abbreviation length used by 'describe'

The default version name for a Git binary is computed by running "git describe" on the commit the binary is made out of, basing on a tag whose name matches "v[0-9]*", e.g. v2.11.0-rc2-2-g7f1dc9.

In the very early days, with 9b88fce ("Makefile: use git-describe to mark the git version.", 2005-12-27), we used "--abbrev=4" to get absolute minimum number of abbreviated commit object name.
This was later changed to match the default minimum of 7 with bf50515 ("Git 1.7.10.1", 2012-05-01).

These days, the "default minimum" scales automatically depending on the size of the repository, and there is no point in specifying a particular abbreviation length; all we wanted since Git 1.7.10.1 days was to get "something reasonable we would use by default".

(That was introduced in Git 2.11: see the last part of "How much of a git sha is generally considered necessary to uniquely identify a change in a given codebase?")

Just drop "--abbrev=<number>" from the invocation of "git describe" and let the command pick what it thinks is appropriate, taking the end user's configuration and the repository contents into account.

Tags:

Embed

Git

Version