</>EmbeddedInterview

What is git bisect and how does it help?

EasyDebugging, Testing & Tools

Short answer: git bisect performs a binary search through the commit history to find the commit that introduced a bug. You mark a known-good and a known-bad commit; git checks out the commit halfway between; you test it and mark it good or bad; git repeats until it identifies the first bad commit. 1,000 commits take only about 10 steps. With a test script that returns 0 (good) or non-zero (bad), git bisect run automates the whole search, even including flashing and testing on hardware.

git bisect start
git bisect bad                 # current HEAD has the bug
git bisect good v2.3.0         # this release was fine
# build, flash, test... then:
git bisect good                # or: git bisect bad, or: git bisect skip (doesn't build)
# ...repeat until git prints "<sha> is the first bad commit"
git bisect reset               # return to where you started

# automated, e.g. with a HIL test script
git bisect run ./ci/flash_and_test.sh

Tips: small, focused commits make the result meaningful; every commit should build (otherwise use git bisect skip); for intermittent bugs, make the test repeat enough times to be confident a "good" verdict is really good; and bisecting configuration or dependency changes (toolchain, SDK versions) is also possible by bisecting the commits that changed them.

Related git tools: git log -S "symbol" (find commits that added or removed text), git blame (who changed a line and why), git log -p --follow file.