NAME

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

SYNOPSIS

  1. git bisect <subcommand> <options>

DESCRIPTION

The command takes various subcommands, and different options dependingon the subcommand:

  1. git bisect start [--term-{old,good}=<term> --term-{new,bad}=<term>]
  2. [--no-checkout] [<bad> [<good>...]] [--] [<paths>...]
  3. git bisect (bad|new|<term-new>) [<rev>]
  4. git bisect (good|old|<term-old>) [<rev>...]
  5. git bisect terms [--term-good | --term-bad]
  6. git bisect skip [(<rev>|<range>)...]
  7. git bisect reset [<commit>]
  8. git bisect (visualize|view)
  9. git bisect replay <logfile>
  10. git bisect log
  11. git bisect run <cmd>...
  12. git bisect help

This command uses a binary search algorithm to find which commit inyour project’s history introduced a bug. You use it by first tellingit a "bad" commit that is known to contain the bug, and a "good"commit that is known to be before the bug was introduced. Then gitbisect picks a commit between those two endpoints and asks youwhether the selected commit is "good" or "bad". It continues narrowingdown the range until it finds the exact commit that introduced thechange.

In fact, git bisect can be used to find the commit that changedany property of your project; e.g., the commit that fixed a bug, orthe commit that caused a benchmark’s performance to improve. Tosupport this more general usage, the terms "old" and "new" can be usedin place of "good" and "bad", or you can choose your own terms. Seesection "Alternate terms" below for more information.

Basic bisect commands: start, bad, good

As an example, suppose you are trying to find the commit that broke afeature that was known to work in version v2.6.13-rc2 of yourproject. You start a bisect session as follows:

  1. $ git bisect start
  2. $ git bisect bad # Current version is bad
  3. $ git bisect good v2.6.13-rc2 # v2.6.13-rc2 is known to be good

Once you have specified at least one bad and one good commit, gitbisect selects a commit in the middle of that range of history,checks it out, and outputs something similar to the following:

  1. Bisecting: 675 revisions left to test after this (roughly 10 steps)

You should now compile the checked-out version and test it. If thatversion works correctly, type

  1. $ git bisect good

If that version is broken, type

  1. $ git bisect bad

Then git bisect will respond with something like

  1. Bisecting: 337 revisions left to test after this (roughly 9 steps)

Keep repeating the process: compile the tree, test it, and dependingon whether it is good or bad run git bisect good or git bisect badto ask for the next commit that needs testing.

Eventually there will be no more revisions left to inspect, and thecommand will print out a description of the first bad commit. Thereference refs/bisect/bad will be left pointing at that commit.

Bisect reset

After a bisect session, to clean up the bisection state and return tothe original HEAD, issue the following command:

  1. $ git bisect reset

By default, this will return your tree to the commit that was checkedout before git bisect start. (A new git bisect start will also dothat, as it cleans up the old bisection state.)

With an optional argument, you can return to a different commitinstead:

  1. $ git bisect reset <commit>

For example, git bisect reset bisect/bad will check out the firstbad revision, while git bisect reset HEAD will leave you on thecurrent bisection commit and avoid switching commits at all.

Alternate terms

Sometimes you are not looking for the commit that introduced abreakage, but rather for a commit that caused a change between someother "old" state and "new" state. For example, you might be lookingfor the commit that introduced a particular fix. Or you might belooking for the first commit in which the source-code filenames werefinally all converted to your company’s naming standard. Or whatever.

In such cases it can be very confusing to use the terms "good" and"bad" to refer to "the state before the change" and "the state afterthe change". So instead, you can use the terms "old" and "new",respectively, in place of "good" and "bad". (But note that you cannotmix "good" and "bad" with "old" and "new" in a single session.)

In this more general usage, you provide git bisect with a "new"commit that has some property and an "old" commit that doesn’t have thatproperty. Each time git bisect checks out a commit, you test if thatcommit has the property. If it does, mark the commit as "new";otherwise, mark it as "old". When the bisection is done, git bisectwill report which commit introduced the property.

To use "old" and "new" instead of "good" and bad, you must run gitbisect start without commits as argument and then run the followingcommands to add the commits:

  1. git bisect old [<rev>]

to indicate that a commit was before the sought change, or

  1. git bisect new [<rev>...]

to indicate that it was after.

To get a reminder of the currently used terms, use

  1. git bisect terms

You can get just the old (respectively new) term with git bisect terms—term-old or git bisect terms —term-good.

If you would like to use your own terms instead of "bad"/"good" or"new"/"old", you can choose any names you like (except existing bisectsubcommands like reset, start, …​) by starting thebisection using

  1. git bisect start --term-old <term-old> --term-new <term-new>

For example, if you are looking for a commit that introduced aperformance regression, you might use

  1. git bisect start --term-old fast --term-new slow

Or if you are looking for the commit that fixed a bug, you might use

  1. git bisect start --term-new fixed --term-old broken

Then, use git bisect <term-old> and git bisect <term-new> insteadof git bisect good and git bisect bad to mark commits.

Bisect visualize/view

To see the currently remaining suspects in gitk, issue the followingcommand during the bisection process (the subcommand view can be usedas an alternative to visualize):

  1. $ git bisect visualize

If the DISPLAY environment variable is not set, git log is usedinstead. You can also give command-line options such as -p and—stat.

  1. $ git bisect visualize --stat

Bisect log and bisect replay

After having marked revisions as good or bad, issue the followingcommand to show what has been done so far:

  1. $ git bisect log

If you discover that you made a mistake in specifying the status of arevision, you can save the output of this command to a file, edit it toremove the incorrect entries, and then issue the following commands toreturn to a corrected state:

  1. $ git bisect reset
  2. $ git bisect replay that-file

Avoiding testing a commit

If, in the middle of a bisect session, you know that the suggestedrevision is not a good one to test (e.g. it fails to build and youknow that the failure does not have anything to do with the bug youare chasing), you can manually select a nearby commit and test thatone instead.

For example:

  1. $ git bisect good/bad # previous round was good or bad.
  2. Bisecting: 337 revisions left to test after this (roughly 9 steps)
  3. $ git bisect visualize # oops, that is uninteresting.
  4. $ git reset --hard HEAD~3 # try 3 revisions before what
  5. # was suggested

Then compile and test the chosen revision, and afterwards markthe revision as good or bad in the usual manner.

Bisect skip

Instead of choosing a nearby commit by yourself, you can ask Git to doit for you by issuing the command:

  1. $ git bisect skip # Current version cannot be tested

However, if you skip a commit adjacent to the one you are looking for,Git will be unable to tell exactly which of those commits was thefirst bad one.

You can also skip a range of commits, instead of just one commit,using range notation. For example:

  1. $ git bisect skip v2.5..v2.6

This tells the bisect process that no commit after v2.5, up to andincluding v2.6, should be tested.

Note that if you also want to skip the first commit of the range youwould issue the command:

  1. $ git bisect skip v2.5 v2.5..v2.6

This tells the bisect process that the commits between v2.5 andv2.6 (inclusive) should be skipped.

Cutting down bisection by giving more parameters to bisect start

You can further cut down the number of trials, if you know what part ofthe tree is involved in the problem you are tracking down, by specifyingpath parameters when issuing the bisect start command:

  1. $ git bisect start -- arch/i386 include/asm-i386

If you know beforehand more than one good commit, you can narrow thebisect space down by specifying all of the good commits immediately afterthe bad commit when issuing the bisect start command:

  1. $ git bisect start v2.6.20-rc6 v2.6.20-rc4 v2.6.20-rc1 --
  2. # v2.6.20-rc6 is bad
  3. # v2.6.20-rc4 and v2.6.20-rc1 are good

Bisect run

If you have a script that can tell if the current source code is goodor bad, you can bisect by issuing the command:

  1. $ git bisect run my_script arguments

Note that the script (my_script in the above example) should exitwith code 0 if the current source code is good/old, and exit with acode between 1 and 127 (inclusive), except 125, if the current sourcecode is bad/new.

Any other exit code will abort the bisect process. It should be notedthat a program that terminates via exit(-1) leaves $? = 255, (see theexit(3) manual page), as the value is chopped with & 0377.

The special exit code 125 should be used when the current source codecannot be tested. If the script exits with this code, the currentrevision will be skipped (see git bisect skip above). 125 was chosenas the highest sensible value to use for this purpose, because 126 and 127are used by POSIX shells to signal specific error status (127 is forcommand not found, 126 is for command found but not executable—​thesedetails do not matter, as they are normal errors in the script, as far asbisect run is concerned).

You may often find that during a bisect session you want to havetemporary modifications (e.g. s/#define DEBUG 0/#define DEBUG 1/ in aheader file, or "revision that does not have this commit needs thispatch applied to work around another problem this bisection is notinterested in") applied to the revision being tested.

To cope with such a situation, after the inner git bisect finds thenext revision to test, the script can apply the patchbefore compiling, run the real test, and afterwards decide if therevision (possibly with the needed patch) passed the test and thenrewind the tree to the pristine state. Finally the script should exitwith the status of the real test to let the git bisect run command loopdetermine the eventual outcome of the bisect session.

OPTIONS

  • —no-checkout

Do not checkout the new working tree at each iteration of the bisectionprocess. Instead just update a special reference named BISECT_HEAD to makeit point to the commit that should be tested.

This option may be useful when the test you would perform in each stepdoes not require a checked out tree.

If the repository is bare, —no-checkout is assumed.

EXAMPLES

  • Automatically bisect a broken build between v1.2 and HEAD:
  1. $ git bisect start HEAD v1.2 -- # HEAD is bad, v1.2 is good
  2. $ git bisect run make # "make" builds the app
  3. $ git bisect reset # quit the bisect session
  • Automatically bisect a test failure between origin and HEAD:
  1. $ git bisect start HEAD origin -- # HEAD is bad, origin is good
  2. $ git bisect run make test # "make test" builds and tests
  3. $ git bisect reset # quit the bisect session
  • Automatically bisect a broken test case:
  1. $ cat ~/test.sh
  2. #!/bin/sh
  3. make || exit 125 # this skips broken builds
  4. ~/check_test_case.sh # does the test case pass?
  5. $ git bisect start HEAD HEAD~10 -- # culprit is among the last 10
  6. $ git bisect run ~/test.sh
  7. $ git bisect reset # quit the bisect session

Here we use a test.sh custom script. In this script, if makefails, we skip the current commit.check_test_case.sh should exit 0 if the test case passes,and exit 1 otherwise.

It is safer if both test.sh and check_test_case.sh areoutside the repository to prevent interactions between the bisect,make and test processes and the scripts.

  • Automatically bisect with temporary modifications (hot-fix):
  1. $ cat ~/test.sh
  2. #!/bin/sh
  3.  
  4. # tweak the working tree by merging the hot-fix branch
  5. # and then attempt a build
  6. if git merge --no-commit hot-fix &&
  7. make
  8. then
  9. # run project specific test and report its status
  10. ~/check_test_case.sh
  11. status=$?
  12. else
  13. # tell the caller this is untestable
  14. status=125
  15. fi
  16.  
  17. # undo the tweak to allow clean flipping to the next commit
  18. git reset --hard
  19.  
  20. # return control
  21. exit $status

This applies modifications from a hot-fix branch before each test run,e.g. in case your build or test environment changed so that olderrevisions may need a fix which newer ones have already. (Make sure thehot-fix branch is based off a commit which is contained in all revisionswhich you are bisecting, so that the merge does not pull in too much, oruse git cherry-pick instead of git merge.)

  • Automatically bisect a broken test case:
  1. $ git bisect start HEAD HEAD~10 -- # culprit is among the last 10
  2. $ git bisect run sh -c "make || exit 125; ~/check_test_case.sh"
  3. $ git bisect reset # quit the bisect session

This shows that you can do without a run script if you write the teston a single line.

  • Locate a good region of the object graph in a damaged repository
  1. $ git bisect start HEAD <known-good-commit> [ <boundary-commit> ... ] --no-checkout
  2. $ git bisect run sh -c '
  3. GOOD=$(git for-each-ref "--format=%(objectname)" refs/bisect/good-*) &&
  4. git rev-list --objects BISECT_HEAD --not $GOOD >tmp.$$ &&
  5. git pack-objects --stdout >/dev/null <tmp.$$
  6. rc=$?
  7. rm -f tmp.$$
  8. test $rc = 0'
  9.  
  10. $ git bisect reset # quit the bisect session

In this case, when git bisect run finishes, bisect/bad will refer to a commit thathas at least one parent whose reachable graph is fully traversable in the senserequired by git pack objects.

  • Look for a fix instead of a regression in the code
  1. $ git bisect start
  2. $ git bisect new HEAD # current commit is marked as new
  3. $ git bisect old HEAD~10 # the tenth commit from now is marked as old

or:

  1. $ git bisect start --term-old broken --term-new fixed
  2. $ git bisect fixed
  3. $ git bisect broken HEAD~10

Getting help

Use git bisect to get a short usage description, and git bisecthelp or git bisect -h to get a long usage description.

SEE ALSO

Fighting regressions with git bisect,git-blame[1].

GIT

Part of the git[1] suite