Sub topic 11
Skipping untestable commits, logging, and replaying
This chapter assumes you can run a manual bisect and write a git bisect run script. You should also know that good and bad mark commits, and that git bisect reset ends a session. Here you will handle commits that cannot be tested, read the record of a session, and replay that record.
Why some commits cannot be tested
Bisect picks the midpoint commit mechanically. It does not know whether that commit builds. A commit in the middle of a feature branch may have a half-finished change that breaks compilation. If you mark it bad, you blame the wrong commit. If you mark it good, you hide the real regression.
The correct answer is "I can't tell". Git calls this skipping.
Skipping by hand
When Git checks out a commit you cannot test, run:
git bisect skipGit removes that commit from consideration and checks out a nearby one. You can also skip a specific commit or a range without checking it out first:
git bisect skip 3f2a9c1
git bisect skip v1.3.0..v1.3.4Skipping has a cost. If the first bad commit sits next to skipped commits, Git may be unable to name a single culprit. It then prints a list of candidates. Treat that list as a smaller search area and inspect it yourself.
Skipping in a script
An automated bisect skips with exit code 125. This is the convention from the earlier chapter, repeated here because it is the usual place to apply it:
#!/bin/sh
# test-regression.sh
make -s || exit 125 # cannot build: tell bisect to skip this commit
./run-tests.sh # exit 0 = good, exit 1 to 127 (except 125) = badExit codes above 127 stop the whole bisect. A script that crashes with a signal, or that cannot be found, can therefore end your session early instead of marking commits wrongly. That is a safer failure than a silent wrong answer.
Reading the log
Git records each decision you make during a session. Print the record with:
git bisect logThe output is a list of the commands that produced the current state. It looks like this:
git bisect start
# status: waiting for both good and bad commits
# bad: [9d1c4e2...] Add export option
git bisect bad 9d1c4e2...
# good: [a7b3f10...] Release 1.2.0
git bisect good a7b3f10...
# skip: [5c88d0a...] Refactor build scripts
git bisect skip 5c88d0a...Lines that start with # are comments. The other lines are commands you could run again. The log is useful for three reasons:
- It shows which commits you marked, so you can find a wrong answer.
- It documents how you reached a conclusion, which you can paste into a bug report.
- It can be saved and replayed.
Replaying a session
Save the log to a file, then feed it back to Git:
git bisect log > bisect-session.txt
git bisect reset
# later, or on another clone of the same repository:
git bisect replay bisect-session.txtgit bisect replay runs the commands in the file and leaves you at the same point in the search. The commit hashes must exist in the repository where you replay.
Replay also lets you fix a mistake. Suppose you marked one commit good that was really bad. Edit that line in the file, then replay. You do not need to repeat every test you already did correctly.
Common errors
- Marking an unbuildable commit
bad. Git then reports a commit that only failed to compile. Useskip. - Forgetting
git bisect reset. You stay on a detached checkout in the middle of history. Your next commit would land there. - Replaying in a repository that lacks the commits. Fetch the missing history first.
- Expecting a single answer after many skips. Git may return a range of candidates. Narrow it by hand with
git logandgit show.
Summary of commands
| Command | Purpose |
|---|---|
| `git bisect skip` | Skip the current commit |
| `git bisect skip <rev or range>` | Skip chosen commits without checking them out |
| `git bisect log` | Print the decisions made so far |
| `git bisect replay <file>` | Re-run a saved log |
| `git bisect reset` | End the session and restore the original checkout |
Exercise
You are bisecting and Git checks out a commit where make fails because of a half-finished refactor. What should you do, and what exit code does your git bisect run script return for this case? Then explain how you would fix an earlier mistaken good mark.
Show answer
Run git bisect skip by hand. In a script, return exit code 125 when the build fails. To fix a mistaken mark, save the log with git bisect log > session.txt, edit the wrong line, run git bisect reset, then run git bisect replay session.txt.
Exercise
After several skips, Git prints a list of possible first bad commits instead of one. Why does this happen?
Show answer
The skipped commits sit next to the real boundary between good and bad. Without a test result for them, Git cannot tell which one introduced the bug, so it reports every candidate it could not exclude.