Binary search git bisect

suggest change

git bisect allows you to find which commit introduced a bug using a binary search.

Start a bisect session by giving two commits: a good commit from before the bug, and a bad commit from after it. The bad commit is usually HEAD.

# start the git bisect session
$ git bisect start

# give a commit where the bug doesn't exist
$ git bisect good 49c747d

# give a commit where the bug exists
$ git bisect bad HEAD

git starts a binary search: it picks a commit about halfway in between and checks that revision out. Inspect the code to decide whether that revision is good or bad:

# tell git the revision is good,
# which means it doesn't contain the bug
$ git bisect good

# if the revision contains the bug,
# then tell git it's bad
$ git bisect bad

git keeps bisecting the remaining range. It ends on the first bad revision, which is the commit that introduced the bug unless a good or bad mark was wrong.

Afterwards, run git bisect reset to end the session and return to the commit you were on before the bisect. git bisect checks out commits as it goes, so you are in detached HEAD until you reset.

$ git bisect reset

If you have a script that can check for the bug, you can automate the process with:

$ git bisect run [script] [arguments]

Where [script] is the path to your script and [arguments] is any arguments that should be passed to your script.

Running this command will automatically run through the binary search, executing git bisect good or git bisect bad at each step depending on the exit code of your script. Exiting with 0 indicates good, while exiting with 1-124, 126, or 127 indicates bad. 125 indicates that the script cannot test that revision (which will trigger a git bisect skip).

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents