Sub topic 11
Using custom terms instead of good and bad
This chapter assumes you can run a manual git bisect session (start, good, bad, reset) and that you know the exit code contract for git bisect run: 0 means good, 1 to 127 except 125 means bad, 125 means skip, and 128 or higher aborts the search. The chapter shows how to rename the two states so bisect fits questions other than "which commit broke this?".
Why the words "good" and "bad" get in the way
Bisect finds the boundary between two kinds of commits. Commits on one side have some property, and commits on the other side don't. Regression hunting is the common case, so Git's default words are "good" for before the bug and "bad" for after it.
Many useful questions don't fit those words:
- Which commit fixed this bug? The older commits are the broken ones.
- Which commit made the build faster?
- Which commit first added a deprecation warning?
- Which commit changed the output format of a command?
In each case, calling the old state "good" is wrong or confusing. For the fix question, a commit that still has the bug is the old state, but "good" suggests the opposite. If you run a bisect with the wrong words, you can easily mark commits backwards, and Git will then report a meaningless result with full confidence.
Git solves this with two neutral names and with custom terms.
The built-in neutral names: old and new
Every bisect session accepts old and new as synonyms for good and bad.
| Default term | Neutral name | Meaning |
|---|---|---|
| good | old | The state before the change you are looking for |
| bad | new | The state after the change you are looking for |
The search looks for the first new commit, in the same way that a default session looks for the first bad commit. The position of the commits decides which word applies, not any judgment of quality. old is the state at the older endpoint, and new is the state at the newer endpoint.
A session that uses old and new looks like this:
git bisect start
git bisect old v1.4.0
git bisect new HEADGit checks out a commit in the middle, and you test it and answer with git bisect old or git bisect new.
You can't mix the two vocabularies in one session. If you start with old and new, Git can still accept good and bad as synonyms in some versions, but you should pick one vocabulary and stay with it. Mixing is where mistakes creep in.
Defining your own terms
Neutral names help, but a session about a fix reads better if the words are "broken" and "fixed". You choose them when you start the session with --term-old and --term-new:
git bisect start --term-old=broken --term-new=fixedThis starts a session in which the old state is called broken and the new state is called fixed. Git creates two new subcommands for the session, git bisect broken and git bisect fixed. You then mark the endpoints with them:
git bisect broken v1.4.0
git bisect fixed HEADGit checks out the commit halfway between them. You test it and answer with git bisect broken or git bisect fixed. When the search ends, Git uses your word in its message:
3f9a1c2d8e7b6a5f4e3d2c1b0a9f8e7d6c5b4a39 is the first fixed commitThe message names the first commit in the new state, which here is the commit that fixed the bug.
The options also have the long synonyms --term-good and --term-bad. They behave exactly like --term-old and --term-new. Prefer the old/new spelling, because it keeps the meaning of each option clear: the term is a name for a position in history.
Rules for custom terms
- Both terms must be given together. A session can't rename only one of them.
- The two terms must differ from each other.
- A term can't be the name of a bisect subcommand, such as
start,run,reset,log,replay,skip,terms,visualize, orview. Git rejects it. - Pick short words with no spaces, because they become subcommand names.
If you forget which words a session uses, ask Git:
git bisect termsThis prints the two terms for the current session. You can also ask for just one of them:
git bisect terms --term-old
git bisect terms --term-newThe subcommand is useful in scripts and when you return to a session after a break.
Custom terms and git bisect run
This is the part that catches people. Custom terms change what you type and what Git prints. They do not change the exit code contract of git bisect run.
The exit codes map to positions in history, not to words:
| Exit code | Meaning to Git | With the broken/fixed terms |
|---|---|---|
| 0 | old state | broken |
| 1 to 127, except 125 | new state | fixed |
| 125 | skip this commit | skip this commit |
| 128 and above | abort the search | abort the search |
So when the old state is broken, a script that exits 0 is saying "this commit is broken". That is the reverse of the usual habit where a passing test means success.
The consequence is that for a "find the fix" hunt, your script's natural exit status must be flipped. A passing regression test means the bug is gone, which is the new state, so the script must exit with a code from 1 to 127.
A worked example: finding the commit that fixed a bug
Suppose v1.4.0 has a bug, and main no longer has it. You want the commit that fixed it, perhaps to backport it to a release branch.
The reproducer script has to report the old state (bug present) with 0 and the new state (bug gone) with 1. It must still report build failures as 125.
#!/bin/sh
# check_fixed.sh
# exit 0 = old state: the bug is still present (broken)
# exit 1 = new state: the bug is gone (fixed)
# exit 125 = cannot test this commit
make >/dev/null 2>&1 || exit 125
if ./run_regression_test.sh; then
# The test passes, so the bug is gone.
exit 1
else
# The test fails, so the bug is still there.
exit 0
fiOne point needs care. In the else branch, any failure of the test counts as "still broken", including a crash of the test harness itself. If the harness can fail for reasons other than the bug, check for those first and exit 125 for them, as you did in the earlier chapters on the reproducer script.
Keep the script outside the working tree, for example in ~/bin, so that checkouts during the search don't replace or remove it. Then run the whole session:
git bisect start --term-old=broken --term-new=fixed
git bisect broken v1.4.0
git bisect fixed main
git bisect run ~/bin/check_fixed.sh
git bisect resetBefore the search starts, validate the script on both endpoints as you would for an ordinary regression hunt. The expected results are reversed:
git checkout v1.4.0 && ~/bin/check_fixed.sh; echo "old endpoint exit: $?"
git checkout main && ~/bin/check_fixed.sh; echo "new endpoint exit: $?"The first command should print old endpoint exit: 0, and the second should print new endpoint exit: 1. If you see the opposite, the script logic is inverted, and the search would walk toward the wrong answer.
The sequence of states during a session looks like this:
Other uses of custom terms
The same technique works for any property that changes once in history.
Finding when output changed:
git bisect start --term-old=original --term-new=changedFinding when a performance improvement landed, with a script that times a benchmark and exits 1 if it is faster than a threshold:
git bisect start --term-old=slow --term-new=fastIn every case, the script exits 0 for the older property and 1 for the newer one. Write that mapping as a comment at the top of the script. A future reader, or you in three weeks, will thank you.
Logging and replaying sessions with custom terms
The session log records the terms, so a replay restores them. Saving and replaying works as it does for ordinary sessions:
git bisect log > fixed-hunt.log
git bisect reset
git bisect replay fixed-hunt.logAfter the replay, git bisect terms shows broken and fixed again. You don't have to repeat the --term-old and --term-new options.
Common errors
- Using good or bad in a custom session. If you started with
--term-old=broken --term-new=fixed, thengit bisect badis rejected with an error that tells you the terms in use. Rungit bisect termsto see them. - Forgetting that exit 0 means old. In a fix hunt, a script that exits 0 on success reports "broken" for the commits that are in fact fixed. The search then converges on the wrong boundary, usually the newest commit or the oldest one. Always validate on the endpoints.
- Swapping the endpoints. Marking
v1.4.0asfixedandmainasbrokengives Git an inconsistent picture. Git stops with a message that the merge base must be an old commit or similar, or it reports a result that makes no sense. The old term goes on the older commit. - Choosing a reserved word. Terms such as
resetorskipclash with subcommands and are refused. - Assuming the property changes only once. Bisect assumes that once the state flips from old to new, it stays new. If a bug was fixed, reintroduced, and fixed again, the search finds some boundary, but not necessarily the one you want. Check the result by testing the commit before and after it by hand.
Summary
oldandneware always available as neutral names for the two sides of a bisect.--term-oldand--term-newrename the two states for one session, and Git adds matching subcommands.git bisect termsshows the terms in use.git bisect runkeeps its exit code contract: 0 is the old state, 1 to 127 except 125 is the new state.- For fix hunts, invert the script's natural result and validate it on both endpoints before searching.
Exercise
You want to find the commit that fixed a crash. v2.0.0 crashes and main does not. You start a session with git bisect start --term-old=crashing --term-new=stable. Your script runs ./crash_test.sh, which exits 0 when the program does not crash. What exit code should your script return for a commit where the program does not crash, and why? What would happen if the script returned 0 for that commit?
Show answer
It should return 1 (any value from 1 to 127 except 125). A commit without the crash belongs to the new state, stable, and git bisect run maps exit codes 1 to 127 (except 125) to the new term. If the script returned 0, Git would treat the commit as crashing, the old state. The search would then move toward newer commits as if every non-crashing commit were still broken, and it would end at the wrong commit or at an endpoint, with a meaningless result.
Exercise
During a custom-term session you forget which words you chose. Which command shows them, and what happens if you type git bisect bad in a session that uses broken and fixed?
Show answer
git bisect terms prints the two terms (add --term-old or --term-new to print just one). Typing git bisect bad in that session is rejected with an error saying the session uses the terms broken and fixed, so no commit is marked.