Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

version

embed

I want to introduce a versioning constant grabbed from the version in Git. I know how to do this -- in a very hackish way in svn --

any ideas on how to do this with Git?

like image 651
qodeninja Avatar asked Jun 23 '11 18:06

qodeninja


People also ask

What is build number in git?

The build number is the number of commits between the current commit and the tag. To guarantee an increasing build number, all these conditions must be satisfied: Make all releases from the same Git repository. Make all releases from the same branch.

How do I add a version number to github?

By using tags: Tags in Git can be used to add a version number. adds a version tag of v1. 5.0-beta to your current Git repository.

How do I keep git versioning?

Git can be enabled on a specific folder/directory on your file system to version files within that directory (including sub-directories). In git (and other version control systems) terms, this “tracked folder” is called a repository (which formally is a specific data structure storing versioning information).


2 Answers

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
like image 101
Niels Abildgaard Avatar answered Nov 06 '22 20:11

Niels Abildgaard


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.

like image 23
kubi Avatar answered Nov 06 '22 20:11

kubi