1GIT-BISECT(1)                     Git Manual                     GIT-BISECT(1)
2
3
4

NAME

6       git-bisect - Use binary search to find the commit that introduced a bug
7

SYNOPSIS

9       git bisect <subcommand> <options>
10

DESCRIPTION

12       The command takes various subcommands, and different options depending
13       on the subcommand:
14
15           git bisect start [--term-(new|bad)=<term-new> --term-(old|good)=<term-old>]
16                  [--no-checkout] [--first-parent] [<bad> [<good>...]] [--] [<paths>...]
17           git bisect (bad|new|<term-new>) [<rev>]
18           git bisect (good|old|<term-old>) [<rev>...]
19           git bisect terms [--term-good | --term-bad]
20           git bisect skip [(<rev>|<range>)...]
21           git bisect reset [<commit>]
22           git bisect (visualize|view)
23           git bisect replay <logfile>
24           git bisect log
25           git bisect run <cmd> [<arg>...]
26           git bisect help
27
28       This command uses a binary search algorithm to find which commit in
29       your project’s history introduced a bug. You use it by first telling it
30       a "bad" commit that is known to contain the bug, and a "good" commit
31       that is known to be before the bug was introduced. Then git bisect
32       picks a commit between those two endpoints and asks you whether the
33       selected commit is "good" or "bad". It continues narrowing down the
34       range until it finds the exact commit that introduced the change.
35
36       In fact, git bisect can be used to find the commit that changed any
37       property of your project; e.g., the commit that fixed a bug, or the
38       commit that caused a benchmark’s performance to improve. To support
39       this more general usage, the terms "old" and "new" can be used in place
40       of "good" and "bad", or you can choose your own terms. See section
41       "Alternate terms" below for more information.
42
43   Basic bisect commands: start, bad, good
44       As an example, suppose you are trying to find the commit that broke a
45       feature that was known to work in version v2.6.13-rc2 of your project.
46       You start a bisect session as follows:
47
48           $ git bisect start
49           $ git bisect bad                 # Current version is bad
50           $ git bisect good v2.6.13-rc2    # v2.6.13-rc2 is known to be good
51
52       Once you have specified at least one bad and one good commit, git
53       bisect selects a commit in the middle of that range of history, checks
54       it out, and outputs something similar to the following:
55
56           Bisecting: 675 revisions left to test after this (roughly 10 steps)
57
58       You should now compile the checked-out version and test it. If that
59       version works correctly, type
60
61           $ git bisect good
62
63       If that version is broken, type
64
65           $ git bisect bad
66
67       Then git bisect will respond with something like
68
69           Bisecting: 337 revisions left to test after this (roughly 9 steps)
70
71       Keep repeating the process: compile the tree, test it, and depending on
72       whether it is good or bad run git bisect good or git bisect bad to ask
73       for the next commit that needs testing.
74
75       Eventually there will be no more revisions left to inspect, and the
76       command will print out a description of the first bad commit. The
77       reference refs/bisect/bad will be left pointing at that commit.
78
79   Bisect reset
80       After a bisect session, to clean up the bisection state and return to
81       the original HEAD, issue the following command:
82
83           $ git bisect reset
84
85       By default, this will return your tree to the commit that was checked
86       out before git bisect start. (A new git bisect start will also do that,
87       as it cleans up the old bisection state.)
88
89       With an optional argument, you can return to a different commit
90       instead:
91
92           $ git bisect reset <commit>
93
94       For example, git bisect reset bisect/bad will check out the first bad
95       revision, while git bisect reset HEAD will leave you on the current
96       bisection commit and avoid switching commits at all.
97
98   Alternate terms
99       Sometimes you are not looking for the commit that introduced a
100       breakage, but rather for a commit that caused a change between some
101       other "old" state and "new" state. For example, you might be looking
102       for the commit that introduced a particular fix. Or you might be
103       looking for the first commit in which the source-code filenames were
104       finally all converted to your company’s naming standard. Or whatever.
105
106       In such cases it can be very confusing to use the terms "good" and
107       "bad" to refer to "the state before the change" and "the state after
108       the change". So instead, you can use the terms "old" and "new",
109       respectively, in place of "good" and "bad". (But note that you cannot
110       mix "good" and "bad" with "old" and "new" in a single session.)
111
112       In this more general usage, you provide git bisect with a "new" commit
113       that has some property and an "old" commit that doesn’t have that
114       property. Each time git bisect checks out a commit, you test if that
115       commit has the property. If it does, mark the commit as "new";
116       otherwise, mark it as "old". When the bisection is done, git bisect
117       will report which commit introduced the property.
118
119       To use "old" and "new" instead of "good" and bad, you must run git
120       bisect start without commits as argument and then run the following
121       commands to add the commits:
122
123           git bisect old [<rev>]
124
125       to indicate that a commit was before the sought change, or
126
127           git bisect new [<rev>...]
128
129       to indicate that it was after.
130
131       To get a reminder of the currently used terms, use
132
133           git bisect terms
134
135       You can get just the old (respectively new) term with git bisect terms
136       --term-old or git bisect terms --term-good.
137
138       If you would like to use your own terms instead of "bad"/"good" or
139       "new"/"old", you can choose any names you like (except existing bisect
140       subcommands like reset, start, ...) by starting the bisection using
141
142           git bisect start --term-old <term-old> --term-new <term-new>
143
144       For example, if you are looking for a commit that introduced a
145       performance regression, you might use
146
147           git bisect start --term-old fast --term-new slow
148
149       Or if you are looking for the commit that fixed a bug, you might use
150
151           git bisect start --term-new fixed --term-old broken
152
153       Then, use git bisect <term-old> and git bisect <term-new> instead of
154       git bisect good and git bisect bad to mark commits.
155
156   Bisect visualize/view
157       To see the currently remaining suspects in gitk, issue the following
158       command during the bisection process (the subcommand view can be used
159       as an alternative to visualize):
160
161           $ git bisect visualize
162
163       Git detects a graphical environment through various environment
164       variables: DISPLAY, which is set in X Window System environments on
165       Unix systems. SESSIONNAME, which is set under Cygwin in interactive
166       desktop sessions. MSYSTEM, which is set under Msys2 and Git for
167       Windows. SECURITYSESSIONID, which may be set on macOS in interactive
168       desktop sessions.
169
170       If none of these environment variables is set, git log is used instead.
171       You can also give command-line options such as -p and --stat.
172
173           $ git bisect visualize --stat
174
175   Bisect log and bisect replay
176       After having marked revisions as good or bad, issue the following
177       command to show what has been done so far:
178
179           $ git bisect log
180
181       If you discover that you made a mistake in specifying the status of a
182       revision, you can save the output of this command to a file, edit it to
183       remove the incorrect entries, and then issue the following commands to
184       return to a corrected state:
185
186           $ git bisect reset
187           $ git bisect replay that-file
188
189   Avoiding testing a commit
190       If, in the middle of a bisect session, you know that the suggested
191       revision is not a good one to test (e.g. it fails to build and you know
192       that the failure does not have anything to do with the bug you are
193       chasing), you can manually select a nearby commit and test that one
194       instead.
195
196       For example:
197
198           $ git bisect good/bad                   # previous round was good or bad.
199           Bisecting: 337 revisions left to test after this (roughly 9 steps)
200           $ git bisect visualize                  # oops, that is uninteresting.
201           $ git reset --hard HEAD~3               # try 3 revisions before what
202                                                   # was suggested
203
204       Then compile and test the chosen revision, and afterwards mark the
205       revision as good or bad in the usual manner.
206
207   Bisect skip
208       Instead of choosing a nearby commit by yourself, you can ask Git to do
209       it for you by issuing the command:
210
211           $ git bisect skip                 # Current version cannot be tested
212
213       However, if you skip a commit adjacent to the one you are looking for,
214       Git will be unable to tell exactly which of those commits was the first
215       bad one.
216
217       You can also skip a range of commits, instead of just one commit, using
218       range notation. For example:
219
220           $ git bisect skip v2.5..v2.6
221
222       This tells the bisect process that no commit after v2.5, up to and
223       including v2.6, should be tested.
224
225       Note that if you also want to skip the first commit of the range you
226       would issue the command:
227
228           $ git bisect skip v2.5 v2.5..v2.6
229
230       This tells the bisect process that the commits between v2.5 and v2.6
231       (inclusive) should be skipped.
232
233   Cutting down bisection by giving more parameters to bisect start
234       You can further cut down the number of trials, if you know what part of
235       the tree is involved in the problem you are tracking down, by
236       specifying path parameters when issuing the bisect start command:
237
238           $ git bisect start -- arch/i386 include/asm-i386
239
240       If you know beforehand more than one good commit, you can narrow the
241       bisect space down by specifying all of the good commits immediately
242       after the bad commit when issuing the bisect start command:
243
244           $ git bisect start v2.6.20-rc6 v2.6.20-rc4 v2.6.20-rc1 --
245                              # v2.6.20-rc6 is bad
246                              # v2.6.20-rc4 and v2.6.20-rc1 are good
247
248   Bisect run
249       If you have a script that can tell if the current source code is good
250       or bad, you can bisect by issuing the command:
251
252           $ git bisect run my_script arguments
253
254       Note that the script (my_script in the above example) should exit with
255       code 0 if the current source code is good/old, and exit with a code
256       between 1 and 127 (inclusive), except 125, if the current source code
257       is bad/new.
258
259       Any other exit code will abort the bisect process. It should be noted
260       that a program that terminates via exit(-1) leaves $? = 255, (see the
261       exit(3) manual page), as the value is chopped with & 0377.
262
263       The special exit code 125 should be used when the current source code
264       cannot be tested. If the script exits with this code, the current
265       revision will be skipped (see git bisect skip above). 125 was chosen as
266       the highest sensible value to use for this purpose, because 126 and 127
267       are used by POSIX shells to signal specific error status (127 is for
268       command not found, 126 is for command found but not executable—these
269       details do not matter, as they are normal errors in the script, as far
270       as bisect run is concerned).
271
272       You may often find that during a bisect session you want to have
273       temporary modifications (e.g. s/#define DEBUG 0/#define DEBUG 1/ in a
274       header file, or "revision that does not have this commit needs this
275       patch applied to work around another problem this bisection is not
276       interested in") applied to the revision being tested.
277
278       To cope with such a situation, after the inner git bisect finds the
279       next revision to test, the script can apply the patch before compiling,
280       run the real test, and afterwards decide if the revision (possibly with
281       the needed patch) passed the test and then rewind the tree to the
282       pristine state. Finally the script should exit with the status of the
283       real test to let the git bisect run command loop determine the eventual
284       outcome of the bisect session.
285

OPTIONS

287       --no-checkout
288           Do not checkout the new working tree at each iteration of the
289           bisection process. Instead just update a special reference named
290           BISECT_HEAD to make it point to the commit that should be tested.
291
292           This option may be useful when the test you would perform in each
293           step does not require a checked out tree.
294
295           If the repository is bare, --no-checkout is assumed.
296
297       --first-parent
298           Follow only the first parent commit upon seeing a merge commit.
299
300           In detecting regressions introduced through the merging of a
301           branch, the merge commit will be identified as introduction of the
302           bug and its ancestors will be ignored.
303
304           This option is particularly useful in avoiding false positives when
305           a merged branch contained broken or non-buildable commits, but the
306           merge itself was OK.
307

EXAMPLES

309       •   Automatically bisect a broken build between v1.2 and HEAD:
310
311               $ git bisect start HEAD v1.2 --      # HEAD is bad, v1.2 is good
312               $ git bisect run make                # "make" builds the app
313               $ git bisect reset                   # quit the bisect session
314
315       •   Automatically bisect a test failure between origin and HEAD:
316
317               $ git bisect start HEAD origin --    # HEAD is bad, origin is good
318               $ git bisect run make test           # "make test" builds and tests
319               $ git bisect reset                   # quit the bisect session
320
321       •   Automatically bisect a broken test case:
322
323               $ cat ~/test.sh
324               #!/bin/sh
325               make || exit 125                     # this skips broken builds
326               ~/check_test_case.sh                 # does the test case pass?
327               $ git bisect start HEAD HEAD~10 --   # culprit is among the last 10
328               $ git bisect run ~/test.sh
329               $ git bisect reset                   # quit the bisect session
330
331           Here we use a test.sh custom script. In this script, if make fails,
332           we skip the current commit.  check_test_case.sh should exit 0 if
333           the test case passes, and exit 1 otherwise.
334
335           It is safer if both test.sh and check_test_case.sh are outside the
336           repository to prevent interactions between the bisect, make and
337           test processes and the scripts.
338
339       •   Automatically bisect with temporary modifications (hot-fix):
340
341               $ cat ~/test.sh
342               #!/bin/sh
343
344               # tweak the working tree by merging the hot-fix branch
345               # and then attempt a build
346               if      git merge --no-commit --no-ff hot-fix &&
347                       make
348               then
349                       # run project specific test and report its status
350                       ~/check_test_case.sh
351                       status=$?
352               else
353                       # tell the caller this is untestable
354                       status=125
355               fi
356
357               # undo the tweak to allow clean flipping to the next commit
358               git reset --hard
359
360               # return control
361               exit $status
362
363           This applies modifications from a hot-fix branch before each test
364           run, e.g. in case your build or test environment changed so that
365           older revisions may need a fix which newer ones have already. (Make
366           sure the hot-fix branch is based off a commit which is contained in
367           all revisions which you are bisecting, so that the merge does not
368           pull in too much, or use git cherry-pick instead of git merge.)
369
370       •   Automatically bisect a broken test case:
371
372               $ git bisect start HEAD HEAD~10 --   # culprit is among the last 10
373               $ git bisect run sh -c "make || exit 125; ~/check_test_case.sh"
374               $ git bisect reset                   # quit the bisect session
375
376           This shows that you can do without a run script if you write the
377           test on a single line.
378
379       •   Locate a good region of the object graph in a damaged repository
380
381               $ git bisect start HEAD <known-good-commit> [ <boundary-commit> ... ] --no-checkout
382               $ git bisect run sh -c '
383                       GOOD=$(git for-each-ref "--format=%(objectname)" refs/bisect/good-*) &&
384                       git rev-list --objects BISECT_HEAD --not $GOOD >tmp.$$ &&
385                       git pack-objects --stdout >/dev/null <tmp.$$
386                       rc=$?
387                       rm -f tmp.$$
388                       test $rc = 0'
389
390               $ git bisect reset                   # quit the bisect session
391
392           In this case, when git bisect run finishes, bisect/bad will refer
393           to a commit that has at least one parent whose reachable graph is
394           fully traversable in the sense required by git pack objects.
395
396       •   Look for a fix instead of a regression in the code
397
398               $ git bisect start
399               $ git bisect new HEAD    # current commit is marked as new
400               $ git bisect old HEAD~10 # the tenth commit from now is marked as old
401
402           or:
403
404           $ git bisect start --term-old broken --term-new fixed
405           $ git bisect fixed
406           $ git bisect broken HEAD~10
407
408   Getting help
409       Use git bisect to get a short usage description, and git bisect help or
410       git bisect -h to get a long usage description.
411

SEE ALSO

413       Fighting regressions with git bisect[1], git-blame(1).
414

GIT

416       Part of the git(1) suite
417

NOTES

419        1. Fighting regressions with git bisect
420           file:///usr/share/doc/git/git-bisect-lk2009.html
421
422
423
424Git 2.43.0                        11/20/2023                     GIT-BISECT(1)
Impressum