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