Many developers are using script to increment build number after each deployment. I don’t like this approach because it provides no info about a builds other than ordering. Since Apple requires this to be increasing number - commits hash can’t be used. But it is possible to use number of commits in current branch - this allows to quickly find a commit for any build deployed.

Also my approach only changes plist in build results, so nothing appear in git status for woking directory.

Autoupdate script

Add script to new build phase: ProjectName > TargetName > Build Phases > New Run Script Phase.

Script can be just dropped as a text here, but I prefer to store it separately, so I am adding:

sh Scripts/

Adding Build Phase

And here is shell script:

# Evgeny Aleksandrov

git=`sh /etc/profile; which git`
branch=`${git} rev-parse --abbrev-ref HEAD`
commits_count=`${git} rev-list ${branch} | wc -l  | tr -d ' '`


echo "Updating ${filepath}"
echo "Current version build ${commits_count}"
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion ${commits_count}" "${filepath}"

Accessing app version and build number in runtime

For every my project I am adding short log of current bundle id + version + build number in AppDelegate.

NSString *bundleId = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
NSString *bundleShortVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
NSString *bundleVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];

NSLog(@"Starting %@ v%@ (%@)", bundleId, bundleShortVersion, bundleVersion);
let bundleId = NSBundle.mainBundle().infoDictionary?["CFBundleIdentifier"] as! NSString
let bundleShortVersion = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as! NSString
let bundleVersion = NSBundle.mainBundle().infoDictionary?["CFBundleVersion"] as! NSString

print("Starting \(bundleId) v\(bundleShortVersion) (\(bundleVersion))")

Finding commit for build number

If you want to get commit hash from build number, add this command to [alias] section of your ~/.gitconfig:

show-rev-number = !sh -c 'git rev-list --reverse HEAD | nl | awk \"{ if(\\$1 == "$0") { print \\$2 }}\"'

And then use it in your working directory (don’t forget to be on your correct release branch):

> git show-rev-number 13