Video in progress

Middle 🌿 Git

The tests passed a month ago and fail now, and there are 300 commits in between. How do you quickly find the commit that broke them?

With git bisect: you mark one "good" and one "bad" commit, and Git does a binary search through the commits between them. For 300 commits, about 8-9 steps are enough, and with a test script the process can be fully automated.

📖 Answer

By hand:

git bisect start
git bisect bad HEAD
git bisect good v3.1.0
# Git moves to the middle commit: you run the test
git bisect good    # or: git bisect bad
# ... a few times ...
# "a7c3e91 is the first bad commit"
git bisect reset

Automatic is the most convenient option. If the script returns 0 it is "good", if it returns 1-127 (other than 125) it is "bad", and 125 means "skip this commit" (for example, it does not build):

git bisect start HEAD v3.1.0
git bisect run ./mvnw -q -Dtest=InvoiceTotalTest test

In a few minutes Git finds the guilty commit by itself.

Practical advice: bisect works well only on a history where every commit builds. "wip, does not compile" commits make bisect struggle. That is one more reason for clean, atomic commits. Do not bisect with a flaky test, because the result will be random.