Get the current GIT branch name for a project in Platformio?

Is there a way to obtain the name of the branch you are currently on from the PlatformIO environment kind of like you can get ${PROJECT_DIR} ?
PROJECT_DIR will give me the folder name of the local cloned GIT repo, but the project has many branches, and when I compile a certain branch I need to get the name of that branch passed to my C/C++ code.
Any help is appreciated. Thanks!



Thank you maxgerhardt, exactly what I needed!

build_flags =
    ; Latest commit hash; `%h` formats it to the short commit hash version:
    !echo '-D BUILD_GIT_COMMIT_HASH=\\"'$(git log -1 --format=%%h)'\\"'

This is for commit hash, you can get a branch name the same way.


build_flags = !echo '-D BUILD_GIT_BRANCH=\\"'$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/\1/")'\\"'

worked for me on OSX (unix)