Include Git tag (or SVN revision) in Arduino sketch?
I struggled with Arduino (ESP32) versioning also.
To make it somewhat more practical and less error prone I made some scripts to assist this process.

Using these scripts is as easy as adding them to your sketch folder and adding

#include "gitTagVersion.h"
...
Serial.println( sketchVersion );


to your Arduino sketch.
That is all that is needed.

The output in the example above will be something like v1.0.0-3-gab3fb04.
That breaks down to tag v1.0.0, we had 3 commits since that tag, at commit ab3fb04.

HOW TO INSTALL AND USE?

There are 2 scripts involved, the first script is a replacement for the Arduino  Verify button.
Save it as verify.sh:

echo "const char * sketchVersion = \"$(git describe --tags --always --dirty)\";" &gt; gitTagVersion.h
~/arduino-1.8.5/arduino --verify test.ino
rm gitTagVersion.h


And the second script replaces the Upload button.
Save it as upload.sh:

echo "const char * sketchVersion = \"$(git describe --tags --always --dirty)\";" &gt; gitTagVersion.h
~/arduino-1.8.5/arduino --upload test.ino --pref custom_DebugLevel=esp32_none
rm gitTagVersion.h


Some things to note about these scripts:


The scripts REPLACE the buttons.
You won’t be able to use the buttons anymore, as these buttons don’t
generate the necessary version info from git.

Using them will result in this error:


  fatal error: gitTagVersion.h:    No such file or directory

The scripts are hardcoded to use Arduino 1.8.5 and have to be updated
if you update your Arduino IDE.
These script only work in Linux with a vanilla Arduino install.
If you use Windows, or are on a Mac the scripts will not work.
To change the debug output (using the LOGx_ESP macros) on ESP32
builds, change the custom_DebugLevel in the Upload script to
esp32_xxxx, where xxxx is none, info, error, debug or verbose.


I posted this also on my blog and esp32.com