Git bisect

Soufiane Oucherrou
3 min readDec 4, 2019

--

git bisect is a tool that allows you to find an offending commit or free bug version of your code through your all commits … If you can find a commit where the code works properly and a commit where it doesn’t, you don’t have to trace down the offending commit by hand; git-bisect will do that for you

How git bisect works !

This command uses a binary search algorithm to find which commit in your project’s history introduced a bug. You use it by first telling it 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 git bisect picks a commit between those two endpoints and asks you whether the selected commit is "good" or "bad". It continues narrowing down the range until it finds the exact commit that introduced the change.

first of all when your run git bisect in first time you will get a helper message shows you all commands that available

git bisect

run git bisect start command, it will tell the git start bisect to watch

git bisect start

now we already know that the version of our code is not working and has bug but we don’t know yet what where exactly the bug, let’s tell bisect this by running a command for this

run git bisect bad

git bisect bad

so the bisect knows this version of commit is bad and we have to find which commit from our list is good commit to debug , how we can do that ?

this is just simple test just to show the function we testing for this article,

before we run git bisect next

we have to run this command git bisect next and it search for the first stable commit and a message will come up to ask you are you sure you want to,

git bisect next

look how our code looks like

basically it delete the code that is not working and shows yous the the version of code that is working and now you can compare your old code that this version and find where is the problem, if this commit is working you can just run this command git bisect good and will moves back to your new version you been working on it and you keep repeating and testing your code and you fix your all bugs,

to end bisecting just run this command git bisect reset

--

--

No responses yet