1GIT-BISECT(1) Git Manual GIT-BISECT(1)
2
3
4
6 git-bisect - Use binary search to find the commit that introduced a bug
7
9 git bisect <subcommand> <options>
10
12 The command takes various subcommands, and different options depending
13 on the subcommand:
14
15 git bisect start [--term-{new,bad}=<term> --term-{old,good}=<term>]
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>...
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 If the DISPLAY environment variable is not set, git log is used
164 instead. You can also give command-line options such as -p and --stat.
165
166 $ git bisect visualize --stat
167
168 Bisect log and bisect replay
169 After having marked revisions as good or bad, issue the following
170 command to show what has been done so far:
171
172 $ git bisect log
173
174 If you discover that you made a mistake in specifying the status of a
175 revision, you can save the output of this command to a file, edit it to
176 remove the incorrect entries, and then issue the following commands to
177 return to a corrected state:
178
179 $ git bisect reset
180 $ git bisect replay that-file
181
182 Avoiding testing a commit
183 If, in the middle of a bisect session, you know that the suggested
184 revision is not a good one to test (e.g. it fails to build and you know
185 that the failure does not have anything to do with the bug you are
186 chasing), you can manually select a nearby commit and test that one
187 instead.
188
189 For example:
190
191 $ git bisect good/bad # previous round was good or bad.
192 Bisecting: 337 revisions left to test after this (roughly 9 steps)
193 $ git bisect visualize # oops, that is uninteresting.
194 $ git reset --hard HEAD~3 # try 3 revisions before what
195 # was suggested
196
197 Then compile and test the chosen revision, and afterwards mark the
198 revision as good or bad in the usual manner.
199
200 Bisect skip
201 Instead of choosing a nearby commit by yourself, you can ask Git to do
202 it for you by issuing the command:
203
204 $ git bisect skip # Current version cannot be tested
205
206 However, if you skip a commit adjacent to the one you are looking for,
207 Git will be unable to tell exactly which of those commits was the first
208 bad one.
209
210 You can also skip a range of commits, instead of just one commit, using
211 range notation. For example:
212
213 $ git bisect skip v2.5..v2.6
214
215 This tells the bisect process that no commit after v2.5, up to and
216 including v2.6, should be tested.
217
218 Note that if you also want to skip the first commit of the range you
219 would issue the command:
220
221 $ git bisect skip v2.5 v2.5..v2.6
222
223 This tells the bisect process that the commits between v2.5 and v2.6
224 (inclusive) should be skipped.
225
226 Cutting down bisection by giving more parameters to bisect start
227 You can further cut down the number of trials, if you know what part of
228 the tree is involved in the problem you are tracking down, by
229 specifying path parameters when issuing the bisect start command:
230
231 $ git bisect start -- arch/i386 include/asm-i386
232
233 If you know beforehand more than one good commit, you can narrow the
234 bisect space down by specifying all of the good commits immediately
235 after the bad commit when issuing the bisect start command:
236
237 $ git bisect start v2.6.20-rc6 v2.6.20-rc4 v2.6.20-rc1 --
238 # v2.6.20-rc6 is bad
239 # v2.6.20-rc4 and v2.6.20-rc1 are good
240
241 Bisect run
242 If you have a script that can tell if the current source code is good
243 or bad, you can bisect by issuing the command:
244
245 $ git bisect run my_script arguments
246
247 Note that the script (my_script in the above example) should exit with
248 code 0 if the current source code is good/old, and exit with a code
249 between 1 and 127 (inclusive), except 125, if the current source code
250 is bad/new.
251
252 Any other exit code will abort the bisect process. It should be noted
253 that a program that terminates via exit(-1) leaves $? = 255, (see the
254 exit(3) manual page), as the value is chopped with & 0377.
255
256 The special exit code 125 should be used when the current source code
257 cannot be tested. If the script exits with this code, the current
258 revision will be skipped (see git bisect skip above). 125 was chosen as
259 the highest sensible value to use for this purpose, because 126 and 127
260 are used by POSIX shells to signal specific error status (127 is for
261 command not found, 126 is for command found but not executable—these
262 details do not matter, as they are normal errors in the script, as far
263 as bisect run is concerned).
264
265 You may often find that during a bisect session you want to have
266 temporary modifications (e.g. s/#define DEBUG 0/#define DEBUG 1/ in a
267 header file, or "revision that does not have this commit needs this
268 patch applied to work around another problem this bisection is not
269 interested in") applied to the revision being tested.
270
271 To cope with such a situation, after the inner git bisect finds the
272 next revision to test, the script can apply the patch before compiling,
273 run the real test, and afterwards decide if the revision (possibly with
274 the needed patch) passed the test and then rewind the tree to the
275 pristine state. Finally the script should exit with the status of the
276 real test to let the git bisect run command loop determine the eventual
277 outcome of the bisect session.
278
280 --no-checkout
281 Do not checkout the new working tree at each iteration of the
282 bisection process. Instead just update a special reference named
283 BISECT_HEAD to make it point to the commit that should be tested.
284
285 This option may be useful when the test you would perform in each
286 step does not require a checked out tree.
287
288 If the repository is bare, --no-checkout is assumed.
289
290 --first-parent
291 Follow only the first parent commit upon seeing a merge commit.
292
293 In detecting regressions introduced through the merging of a
294 branch, the merge commit will be identified as introduction of the
295 bug and its ancestors will be ignored.
296
297 This option is particularly useful in avoiding false positives when
298 a merged branch contained broken or non-buildable commits, but the
299 merge itself was OK.
300
302 · Automatically bisect a broken build between v1.2 and HEAD:
303
304 $ git bisect start HEAD v1.2 -- # HEAD is bad, v1.2 is good
305 $ git bisect run make # "make" builds the app
306 $ git bisect reset # quit the bisect session
307
308 · Automatically bisect a test failure between origin and HEAD:
309
310 $ git bisect start HEAD origin -- # HEAD is bad, origin is good
311 $ git bisect run make test # "make test" builds and tests
312 $ git bisect reset # quit the bisect session
313
314 · Automatically bisect a broken test case:
315
316 $ cat ~/test.sh
317 #!/bin/sh
318 make || exit 125 # this skips broken builds
319 ~/check_test_case.sh # does the test case pass?
320 $ git bisect start HEAD HEAD~10 -- # culprit is among the last 10
321 $ git bisect run ~/test.sh
322 $ git bisect reset # quit the bisect session
323
324 Here we use a test.sh custom script. In this script, if make fails,
325 we skip the current commit. check_test_case.sh should exit 0 if
326 the test case passes, and exit 1 otherwise.
327
328 It is safer if both test.sh and check_test_case.sh are outside the
329 repository to prevent interactions between the bisect, make and
330 test processes and the scripts.
331
332 · Automatically bisect with temporary modifications (hot-fix):
333
334 $ cat ~/test.sh
335 #!/bin/sh
336
337 # tweak the working tree by merging the hot-fix branch
338 # and then attempt a build
339 if git merge --no-commit --no-ff hot-fix &&
340 make
341 then
342 # run project specific test and report its status
343 ~/check_test_case.sh
344 status=$?
345 else
346 # tell the caller this is untestable
347 status=125
348 fi
349
350 # undo the tweak to allow clean flipping to the next commit
351 git reset --hard
352
353 # return control
354 exit $status
355
356 This applies modifications from a hot-fix branch before each test
357 run, e.g. in case your build or test environment changed so that
358 older revisions may need a fix which newer ones have already. (Make
359 sure the hot-fix branch is based off a commit which is contained in
360 all revisions which you are bisecting, so that the merge does not
361 pull in too much, or use git cherry-pick instead of git merge.)
362
363 · Automatically bisect a broken test case:
364
365 $ git bisect start HEAD HEAD~10 -- # culprit is among the last 10
366 $ git bisect run sh -c "make || exit 125; ~/check_test_case.sh"
367 $ git bisect reset # quit the bisect session
368
369 This shows that you can do without a run script if you write the
370 test on a single line.
371
372 · Locate a good region of the object graph in a damaged repository
373
374 $ git bisect start HEAD <known-good-commit> [ <boundary-commit> ... ] --no-checkout
375 $ git bisect run sh -c '
376 GOOD=$(git for-each-ref "--format=%(objectname)" refs/bisect/good-*) &&
377 git rev-list --objects BISECT_HEAD --not $GOOD >tmp.$$ &&
378 git pack-objects --stdout >/dev/null <tmp.$$
379 rc=$?
380 rm -f tmp.$$
381 test $rc = 0'
382
383 $ git bisect reset # quit the bisect session
384
385 In this case, when git bisect run finishes, bisect/bad will refer
386 to a commit that has at least one parent whose reachable graph is
387 fully traversable in the sense required by git pack objects.
388
389 · Look for a fix instead of a regression in the code
390
391 $ git bisect start
392 $ git bisect new HEAD # current commit is marked as new
393 $ git bisect old HEAD~10 # the tenth commit from now is marked as old
394
395 or:
396
397 $ git bisect start --term-old broken --term-new fixed
398 $ git bisect fixed
399 $ git bisect broken HEAD~10
400
401 Getting help
402 Use git bisect to get a short usage description, and git bisect help or
403 git bisect -h to get a long usage description.
404
406 Fighting regressions with git bisect[1], git-blame(1).
407
409 Part of the git(1) suite
410
412 1. Fighting regressions with git bisect
413 file:///usr/share/doc/git/git-bisect-lk2009.html
414
415
416
417Git 2.30.2 2021-03-08 GIT-BISECT(1)