git bisect: find the commit that broke something
Bisect finds the first bad commit by running a reproducible test across a range. Merge commits were not tested.
Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.
TE-08-A: git bisect run locates the first bad commit and bisect reset restores the branch
Setup for this run, inside a throwaway directory (mktemp -d):
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'ok\n' > f.txt; git add f.txt; git commit -qm c0
for i in 1 2; do printf 'ok\n' >> f.txt; git add f.txt; git commit -qm c$i; done
printf 'broken\n' >> f.txt; git add f.txt; git commit -qm c3
printf 'broken\n' >> f.txt; git add f.txt; git commit -qm c4Paths written as ../name sit one level above the repository folder, inside the same throwaway tree, so they do not appear in git status.
Command:
git bisect start >/dev/null; git bisect bad >/dev/null; git bisect good $(git rev-list --max-parents=0 HEAD) >/dev/null; git bisect run bash -c '! grep -q broken f.txt' >../bisect.out 2>&1; echo run=$?; echo bad=$(git log -1 --format=%s $(grep -m1 'first bad commit' ../bisect.out | cut -d' ' -f1)); git bisect reset >/dev/null; echo head=$(git log -1 --format=%s)Recorded output:
run=0
bad=c3
head=c4Standard error from the same run:
Previous HEAD position was 5cf7d3b c3
Switched to branch 'main'Exit status: 0. This is the status recorded in the log for TE-08-A.
TE-08-B: git bisect start with a bad and a good revision, then bisect run with a line-count test, finds the first commit with three lines
Setup for this run, inside a throwaway directory (mktemp -d):
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'l\n' > f.txt; git add f.txt; git commit -qm c0
for i in 1 2 3 4; do printf 'l\n' >> f.txt; git add f.txt; git commit -qm c$i; doneCommand:
git bisect start HEAD $(git rev-list --max-parents=0 HEAD) >/dev/null 2>&1; git bisect run sh -c 'test $(wc -l < f.txt) -lt 3' >/dev/null 2>&1; echo run=$?; echo bad=$(git log -1 --format=%s refs/bisect/bad); git bisect reset >/dev/null 2>&1; echo head=$(git log -1 --format=%s)Recorded output:
run=0
bad=c2
head=c4Exit status: 0. This status and the output match what was written before the run.
TE-08-C: expected non-zero exit: git bisect bad before git bisect start stops with an error
Setup for this run, inside a throwaway directory (mktemp -d):
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > f.txt; git add f.txt; git commit -qm oneCommand:
git bisect badRecorded output: none, standard output was empty.
Standard error from the same run:
You need to start by "git bisect start"Exit status: 1. This status and the output match what was written before the run.
Reading the recorded output
The line run=0 is the exit status of git bisect run. The line bad=c3 is the subject of the commit named on the first bad commit line of the bisect output. By the setup, c3 is the first commit that added the word broken. After git bisect reset, head=c4 is the subject of the newest commit. The standard error lines show the switch back to the branch main. The bad= and head= lines show commit subjects, not ids. The stderr block shows one short id, 5cf7d3b, in git's own message Previous HEAD position was 5cf7d3b c3.
TE-08-B: by the setup, the repository has five commits c0 to c4 and each adds one line, so the third commit c2 is the first with three lines. The test sh -c 'test $(wc -l < f.txt) -lt 3' passes before that and fails from it. git bisect run exited with status 0, the first bad commit read from refs/bisect/bad is c2, and after bisect reset HEAD is c4 again. TE-08-C: git bisect bad before any git bisect start exited with status 1, and standard error reads You need to start by "git bisect start". TE-08-A records an abbreviated commit id in the first line of its standard error. That id changes with the commit contents and differs on each machine and each run.
Limits
The run uses a linear history of five commits and a test with a fixed verdict. Merge commits were not tested. The runs of TE-08-A and TE-08-B end with bisect reset.
Source
Evidence
TE-08-A: git bisect run locates the first bad commit and bisect reset restores the branch
- logs: one recorded log for each row above
- Recorded on: Linux x86_64, kernel 7.3.4; bash 5.1.16; git 2.34.1; python 3.10.12; findutils 4.8.0; grep 3.7; sed 4.8; gawk 5.1.0; tar 1.34; curl 7.81.0. Locale C.UTF-8, TZ=UTC.
Evidence table
| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-08-A | git bisect run locates the first bad commit and bisect reset restores the branch | 0 | PASS | 12af7e8ae0f6 | | TE-08-B | git bisect start with a bad and a good revision, then bisect run with a line-count test, finds the first commit with three lines | 0 | PASS | 043957e6ee81 | | TE-08-C | expected non-zero exit: git bisect bad before git bisect start stops with an error | 1 | PASS | bf9759b1f878 |
Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.
All full guides: All guides