git-p4: add failing test for submit from detached head
[git.git] / Documentation / git-bisect.txt
blobe97f2de21bdc58cc2de35731f7b339353b121bd0
1 git-bisect(1)
2 =============
4 NAME
5 ----
6 git-bisect - Use binary search to find the commit that introduced a bug
9 SYNOPSIS
10 --------
11 [verse]
12 'git bisect' <subcommand> <options>
14 DESCRIPTION
15 -----------
16 The command takes various subcommands, and different options depending
17 on the subcommand:
19  git bisect start [--no-checkout] [<bad> [<good>...]] [--] [<paths>...]
20  git bisect bad [<rev>]
21  git bisect good [<rev>...]
22  git bisect skip [(<rev>|<range>)...]
23  git bisect reset [<commit>]
24  git bisect visualize
25  git bisect replay <logfile>
26  git bisect log
27  git bisect run <cmd>...
28  git bisect help
30 This command uses a binary search algorithm to find which commit in
31 your project's history introduced a bug. You use it by first telling
32 it a "bad" commit that is known to contain the bug, and a "good"
33 commit that is known to be before the bug was introduced. Then `git
34 bisect` picks a commit between those two endpoints and asks you
35 whether the selected commit is "good" or "bad". It continues narrowing
36 down the range until it finds the exact commit that introduced the
37 change.
39 Basic bisect commands: start, bad, good
40 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42 As an example, suppose you are trying to find the commit that broke a
43 feature that was known to work in version `v2.6.13-rc2` of your
44 project. You start a bisect session as follows:
46 ------------------------------------------------
47 $ git bisect start
48 $ git bisect bad                 # Current version is bad
49 $ git bisect good v2.6.13-rc2    # v2.6.13-rc2 is known to be good
50 ------------------------------------------------
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,
54 checks it out, and outputs something similar to the following:
56 ------------------------------------------------
57 Bisecting: 675 revisions left to test after this (roughly 10 steps)
58 ------------------------------------------------
60 You should now compile the checked-out version and test it. If that
61 version works correctly, type
63 ------------------------------------------------
64 $ git bisect good
65 ------------------------------------------------
67 If that version is broken, type
69 ------------------------------------------------
70 $ git bisect bad
71 ------------------------------------------------
73 Then `git bisect` will respond with something like
75 ------------------------------------------------
76 Bisecting: 337 revisions left to test after this (roughly 9 steps)
77 ------------------------------------------------
79 Keep repeating the process: compile the tree, test it, and depending
80 on whether it is good or bad run `git bisect good` or `git bisect bad`
81 to ask for the next commit that needs testing.
83 Eventually there will be no more revisions left to inspect, and the
84 command will print out a description of the first bad commit. The
85 reference `refs/bisect/bad` will be left pointing at that commit.
88 Bisect reset
89 ~~~~~~~~~~~~
91 After a bisect session, to clean up the bisection state and return to
92 the original HEAD, issue the following command:
94 ------------------------------------------------
95 $ git bisect reset
96 ------------------------------------------------
98 By default, this will return your tree to the commit that was checked
99 out before `git bisect start`.  (A new `git bisect start` will also do
100 that, as it cleans up the old bisection state.)
102 With an optional argument, you can return to a different commit
103 instead:
105 ------------------------------------------------
106 $ git bisect reset <commit>
107 ------------------------------------------------
109 For example, `git bisect reset bisect/bad` will check out the first
110 bad revision, while `git bisect reset HEAD` will leave you on the
111 current bisection commit and avoid switching commits at all.
114 Bisect visualize
115 ~~~~~~~~~~~~~~~~
117 To see the currently remaining suspects in 'gitk', issue the following
118 command during the bisection process:
120 ------------
121 $ git bisect visualize
122 ------------
124 `view` may also be used as a synonym for `visualize`.
126 If the 'DISPLAY' environment variable is not set, 'git log' is used
127 instead.  You can also give command-line options such as `-p` and
128 `--stat`.
130 ------------
131 $ git bisect view --stat
132 ------------
134 Bisect log and bisect replay
135 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
137 After having marked revisions as good or bad, issue the following
138 command to show what has been done so far:
140 ------------
141 $ git bisect log
142 ------------
144 If you discover that you made a mistake in specifying the status of a
145 revision, you can save the output of this command to a file, edit it to
146 remove the incorrect entries, and then issue the following commands to
147 return to a corrected state:
149 ------------
150 $ git bisect reset
151 $ git bisect replay that-file
152 ------------
154 Avoiding testing a commit
155 ~~~~~~~~~~~~~~~~~~~~~~~~~
157 If, in the middle of a bisect session, you know that the suggested
158 revision is not a good one to test (e.g. it fails to build and you
159 know that the failure does not have anything to do with the bug you
160 are chasing), you can manually select a nearby commit and test that
161 one instead.
163 For example:
165 ------------
166 $ git bisect good/bad                   # previous round was good or bad.
167 Bisecting: 337 revisions left to test after this (roughly 9 steps)
168 $ git bisect visualize                  # oops, that is uninteresting.
169 $ git reset --hard HEAD~3               # try 3 revisions before what
170                                         # was suggested
171 ------------
173 Then compile and test the chosen revision, and afterwards mark
174 the revision as good or bad in the usual manner.
176 Bisect skip
177 ~~~~~~~~~~~~
179 Instead of choosing a nearby commit by yourself, you can ask Git to do
180 it for you by issuing the command:
182 ------------
183 $ git bisect skip                 # Current version cannot be tested
184 ------------
186 However, if you skip a commit adjacent to the one you are looking for,
187 Git will be unable to tell exactly which of those commits was the
188 first bad one.
190 You can also skip a range of commits, instead of just one commit,
191 using range notation. For example:
193 ------------
194 $ git bisect skip v2.5..v2.6
195 ------------
197 This tells the bisect process that no commit after `v2.5`, up to and
198 including `v2.6`, should be tested.
200 Note that if you also want to skip the first commit of the range you
201 would issue the command:
203 ------------
204 $ git bisect skip v2.5 v2.5..v2.6
205 ------------
207 This tells the bisect process that the commits between `v2.5` and
208 `v2.6` (inclusive) should be skipped.
211 Cutting down bisection by giving more parameters to bisect start
212 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
214 You can further cut down the number of trials, if you know what part of
215 the tree is involved in the problem you are tracking down, by specifying
216 path parameters when issuing the `bisect start` command:
218 ------------
219 $ git bisect start -- arch/i386 include/asm-i386
220 ------------
222 If you know beforehand more than one good commit, you can narrow the
223 bisect space down by specifying all of the good commits immediately after
224 the bad commit when issuing the `bisect start` command:
226 ------------
227 $ git bisect start v2.6.20-rc6 v2.6.20-rc4 v2.6.20-rc1 --
228                    # v2.6.20-rc6 is bad
229                    # v2.6.20-rc4 and v2.6.20-rc1 are good
230 ------------
232 Bisect run
233 ~~~~~~~~~~
235 If you have a script that can tell if the current source code is good
236 or bad, you can bisect by issuing the command:
238 ------------
239 $ git bisect run my_script arguments
240 ------------
242 Note that the script (`my_script` in the above example) should exit
243 with code 0 if the current source code is good/old, and exit with a
244 code between 1 and 127 (inclusive), except 125, if the current source
245 code is bad/new.
247 Any other exit code will abort the bisect process. It should be noted
248 that a program that terminates via `exit(-1)` leaves $? = 255, (see the
249 exit(3) manual page), as the value is chopped with `& 0377`.
251 The special exit code 125 should be used when the current source code
252 cannot be tested. If the script exits with this code, the current
253 revision will be skipped (see `git bisect skip` above). 125 was chosen
254 as the highest sensible value to use for this purpose, because 126 and 127
255 are used by POSIX shells to signal specific error status (127 is for
256 command not found, 126 is for command found but not executable---these
257 details do not matter, as they are normal errors in the script, as far as
258 `bisect run` is concerned).
260 You may often find that during a bisect session you want to have
261 temporary modifications (e.g. s/#define DEBUG 0/#define DEBUG 1/ in a
262 header file, or "revision that does not have this commit needs this
263 patch applied to work around another problem this bisection is not
264 interested in") applied to the revision being tested.
266 To cope with such a situation, after the inner 'git bisect' finds the
267 next revision to test, the script can apply the patch
268 before compiling, run the real test, and afterwards decide if the
269 revision (possibly with the needed patch) passed the test and then
270 rewind the tree to the pristine state.  Finally the script should exit
271 with the status of the real test to let the `git bisect run` command loop
272 determine the eventual outcome of the bisect session.
274 OPTIONS
275 -------
276 --no-checkout::
278 Do not checkout the new working tree at each iteration of the bisection
279 process. Instead just update a special reference named 'BISECT_HEAD' to make
280 it point to the commit that should be tested.
282 This option may be useful when the test you would perform in each step
283 does not require a checked out tree.
285 If the repository is bare, `--no-checkout` is assumed.
287 EXAMPLES
288 --------
290 * 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 ------------
298 * Automatically bisect a test failure between origin and HEAD:
300 ------------
301 $ git bisect start HEAD origin --    # HEAD is bad, origin is good
302 $ git bisect run make test           # "make test" builds and tests
303 $ git bisect reset                   # quit the bisect session
304 ------------
306 * Automatically bisect a broken test case:
308 ------------
309 $ cat ~/test.sh
310 #!/bin/sh
311 make || exit 125                     # this skips broken builds
312 ~/check_test_case.sh                 # does the test case pass?
313 $ git bisect start HEAD HEAD~10 --   # culprit is among the last 10
314 $ git bisect run ~/test.sh
315 $ git bisect reset                   # quit the bisect session
316 ------------
318 Here we use a `test.sh` custom script. In this script, if `make`
319 fails, we skip the current commit.
320 `check_test_case.sh` should `exit 0` if the test case passes,
321 and `exit 1` otherwise.
323 It is safer if both `test.sh` and `check_test_case.sh` are
324 outside the repository to prevent interactions between the bisect,
325 make and test processes and the scripts.
327 * Automatically bisect with temporary modifications (hot-fix):
329 ------------
330 $ cat ~/test.sh
331 #!/bin/sh
333 # tweak the working tree by merging the hot-fix branch
334 # and then attempt a build
335 if      git merge --no-commit hot-fix &&
336         make
337 then
338         # run project specific test and report its status
339         ~/check_test_case.sh
340         status=$?
341 else
342         # tell the caller this is untestable
343         status=125
346 # undo the tweak to allow clean flipping to the next commit
347 git reset --hard
349 # return control
350 exit $status
351 ------------
353 This applies modifications from a hot-fix branch before each test run,
354 e.g. in case your build or test environment changed so that older
355 revisions may need a fix which newer ones have already. (Make sure the
356 hot-fix branch is based off a commit which is contained in all revisions
357 which you are bisecting, so that the merge does not pull in too much, or
358 use `git cherry-pick` instead of `git merge`.)
360 * Automatically bisect a broken test case:
362 ------------
363 $ git bisect start HEAD HEAD~10 --   # culprit is among the last 10
364 $ git bisect run sh -c "make || exit 125; ~/check_test_case.sh"
365 $ git bisect reset                   # quit the bisect session
366 ------------
368 This shows that you can do without a run script if you write the test
369 on a single line.
371 * 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'
383 $ git bisect reset                   # quit the bisect session
384 ------------
386 In this case, when 'git bisect run' finishes, bisect/bad will refer to a commit that
387 has at least one parent whose reachable graph is fully traversable in the sense
388 required by 'git pack objects'.
390 Getting help
391 ~~~~~~~~~~~~
393 Use `git bisect` to get a short usage description, and `git bisect
394 help` or `git bisect -h` to get a long usage description.
396 SEE ALSO
397 --------
398 link:git-bisect-lk2009.html[Fighting regressions with git bisect],
399 linkgit:git-blame[1].
403 Part of the linkgit:git[1] suite