Git and releases for firmware
What goes in the repository, branches and tags, versions built into the image, and releases you can rebuild.
Firmware has to be rebuilt years later
A web service is redeployed every day; an old version barely matters once it is replaced. Firmware is different. Devices stay in the field for ten years, running whatever version they shipped with, and when one of them fails you must answer two questions exactly: which code is it running, and can I build that same binary again to debug it. Version control for firmware is organised around those two questions.
What goes in the repository
| Commit it | Keep it out |
|---|---|
| Source, headers, linker scripts, startup code | Build outputs: build/, *.o, *.elf, *.hex, *.bin, *.map |
Build files: CMake, Make, sdkconfig or prj.conf, Kconfig fragments | IDE workspace state and per-user settings |
The vendor configuration file (.ioc, .mex) and the code it generates | Downloaded toolchains and SDKs, if they are pinned by version elsewhere |
| A file naming the toolchain version, or the CI container that has it | Secrets: signing keys, Wi-Fi passwords, cloud credentials |
| Tests, test fixtures and scripts for flashing and HIL | Large binary assets, unless they are in Git LFS |
A typical .gitignore for a firmware project:
build/
*.o
*.d
*.elf
*.hex
*.bin
*.map
.vscode/*.log
sdkconfig.old
The vendor SDK can be a submodule pinned to a commit, a copy in a vendor/ folder, or a version that the build system downloads (west for Zephyr, the ESP-IDF component manager). Each works; what matters is that a checkout of any commit names exactly one SDK version.
Branches, tags and releases
Most firmware teams do well with a simple scheme:
mainalways builds and passes its tests. Work happens on short-lived feature branches merged through reviewed pull requests.- Tags mark releases: an annotated tag such as
v2.3.0on the exact commit that was built and shipped. Tags are never moved. - Release branches (
release/2.3) exist only for versions you still patch in the field. A fix goes tomainfirst and is cherry-picked back.
Write commit messages for the engineer who runs git log during a field failure: a short summary line in the imperative ("Clear the UART overrun flag before re-arming receive"), then a body saying why. Keep commits small and focused, so one can be reverted or bisected on its own.
Versions built into the image
The running firmware must be able to report its own version, over the console, a BLE characteristic or the update protocol. Generate it at build time from Git, so it can never be forgotten:
git describe --tags --dirty --always
# v2.3.0 exactly the tagged commit
# v2.3.0-14-g8c1f2ab 14 commits after v2.3.0, at commit 8c1f2ab
# v2.3.0-14-g8c1f2ab-dirty ... with uncommitted changes: never ship this
The build writes that string, and the numeric version, into a generated header or a dedicated linker section, so tools can read the version from the binary without running it. A bootloader can also compare versions numerically, for example to refuse a downgrade:
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
/* The version block the build fills in from `git describe` and the release tag. */
typedef struct {
uint8_t major, minor, patch;
const char *describe; /* the full git describe string, for humans */
} fw_version_t;
/* One number that orders versions correctly: 2.10.0 is newer than 2.9.7. */
static uint32_t version_key(const fw_version_t *v) {
return ((uint32_t)v->major << 16) | ((uint32_t)v->minor << 8) | v->patch;
}
/* Anti-rollback: accept an update only if it is at least the oldest version still allowed. */
static bool may_install(const fw_version_t *candidate, const fw_version_t *minimum) {
return version_key(candidate) >= version_key(minimum);
}
int main(void) {
fw_version_t running = { 2, 9, 7, "v2.9.7" };
fw_version_t update = { 2, 10, 0, "v2.10.0-3-g8c1f2ab" };
fw_version_t old = { 2, 8, 1, "v2.8.1" };
printf("running %s, key 0x%06X\n", running.describe, (unsigned)version_key(&running));
printf("install %s? %s\n", update.describe, may_install(&update, &running) ? "yes" : "no");
printf("install %s? %s\n", old.describe, may_install(&old, &running) ? "yes" : "no");
return 0;
}
Compare versions as numbers, never as strings: strcmp("2.10.0", "2.9.7") says 2.10 is older.
Many teams use semantic versioning: MAJOR changes break compatibility (a protocol or settings format), MINOR adds features, PATCH fixes bugs. For devices, decide up front what "compatible" means: with the phone app, the cloud, the bootloader and the stored settings.
Releases you can rebuild
A release is more than a tag. A dependable process:
- Tag the commit; CI builds from the tag in a clean container with the pinned compiler (a different GCC version produces a different binary).
- Refuse to build a release from a dirty tree or an untagged commit.
- Archive the outputs together:
.elf(with debug symbols, to decode field crash dumps),.map,.hexor.bin, the signed update package, their checksums and the release notes. - Write the changelog for people outside the team: what changed for the user, what was fixed, known issues.
Reproducible builds go one step further: the same source and toolchain give a byte-identical binary. Sources of difference to remove are __DATE__ and __TIME__ macros, absolute paths embedded in debug information (-ffile-prefix-map) and unordered file lists in the build.
Finding the commit that broke it
When a version works and a later one does not, git bisect finds the first bad commit by binary search: about seven builds for a hundred commits. With a script that builds, flashes and tests on a board, it runs unattended:
git bisect start v2.4.0 v2.3.0 # bad, then good
git bisect run ./scripts/flash-and-test.sh
git bisect reset
This only works if every commit builds, which is one more reason to keep commits small and main green.
Pitfall: A hotfix built on a laptop and flashed onto customer units, with no tag, is the version nobody can reproduce later. Every binary that leaves the building comes from CI and a tag.
Interview question: How do you make sure you can rebuild the exact firmware a device in the field is running?
Answer: Build every shipped binary in CI from an annotated tag, in a container with a pinned toolchain and a pinned SDK version, and refuse release builds from a dirty tree. Embed the version and git describe string in the image so the device can report it. Archive the ELF with symbols, the map file, the binaries and checksums for every release. Remove sources of non-determinism such as __DATE__ and absolute paths so a rebuild is byte-identical, and verify that by rebuilding a release and comparing.
Build it: know exactly which build is on the sensor
This is step 1 of the module project, a wireless vibration sensor bolted to factory motors to warn of failing bearings before they break. Thousands are installed. When one misbehaves, the first question is "which firmware is it running?", and the update server must never install a test build or roll a sensor back. The answer comes from git describe, which the build bakes into the firmware.
Lab: Parse git describe and decide on an update
git describe --tags --dirty gives strings such as:
| String | Meaning |
|---|---|
v1.4.2 | exactly the release tagged v1.4.2 |
v1.4.2-3-g9f2c1ab | 3 commits after v1.4.2, at commit 9f2c1ab |
v1.4.2-3-g9f2c1ab-dirty | the same, built with uncommitted changes |
version_parse(s, v)fills aversion_tand returns true, or returns false for anything that does not fit the pattern:v, then three decimal numbers separated by dots (each 0 to 999), then optionally-<commits>-g<hash>(the hash is 7 to 12 lowercase hex digits), then optionally-dirty.version_cmp(a, b)returns negative, 0 or positive, comparing major, then minor, then patch, then commits.update_allowed(running, candidate)returns true only when the candidate is a clean release: no commits after its tag, not dirty, and strictly newer than the running version.
Going deeper
Interview question: How would you make sure you can rebuild exactly the firmware running on any unit returned from the field?
Answer: Tag every release in Git, build it in CI from a clean checkout with pinned toolchain and dependency versions, embed the version and commit hash in the binary (and report it over the device's interface), and archive the ELF, map file and symbols with each release. A returned unit's version then identifies its source and its debug symbols.
Interview question: A bug appeared somewhere between two releases 120 commits apart. How do you find the commit that caused it?
Answer: Use git bisect with the old release marked good and the new one bad. Git checks out the midpoint; you build, test and mark it good or bad, and it halves the range each time, so about seven steps for 120 commits. If the test can be automated, for example a script that builds, flashes a board on a test rig and checks the symptom, git bisect run does it unattended. It needs every commit to build, so keeping main green and commits small pays off here.
Interview question: Should a vendor SDK be a Git submodule or copied into the repository?
Answer: Both work if the version is pinned. A submodule keeps the history small and makes upgrades a one-line change, but adds a step to every clone and is awkward if you must patch the SDK. A copy in a vendor folder is simple, survives the upstream repository disappearing, and makes local patches visible in your own history, at the cost of a larger repository. Package managers such as west or the ESP-IDF component manager are a third option. Whatever you choose, record local patches to the SDK clearly so the next upgrade does not silently drop them.
Explain it: In your own words, explain why firmware should report a version generated from Git, and what you can do with it.
Answer: When a device in the field misbehaves, you need to know exactly which code it runs. A version generated at build time from git describe names the release tag, how many commits followed it, the commit hash and whether the tree was dirty, so it always matches the source and cannot be forgotten. With it you can check out that commit, use the archived ELF to decode crash dumps, rebuild the same binary, decide whether the unit needs an update, and let the bootloader refuse downgrades by comparing version numbers.