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