Git bisect
There must be sometime in your life when suddenly you find that your git project is broken by some commit. But how can you find that commit if it is hidden in the history?
Fortunately, git has this awesome functionality called bisect. As explained in the name of the tool,
git bisect is using binary search algorithm
which helps you find the single evil commit that breaks your project. Now, I’m going to show you how
to use it.
First of all, you need a way to test if your project is working. A well composed test suite will be great.
When the testing tool is ready, we can start a bisect session. To tell the tool where to begin, you should
label at lest one good commit and one bad commit. Then, bisect will set you to a commit located in the
middle of the given range. After doing your test, you can lable the current commit as good or bad. And the
tool will set you to another commit according to your action. Now you can do your test and repeat previous
steps for several iteration. And finally, bisect will find the commit that breaks your test.
So, here is the commands you will be using,
git bisect start
git bisect bad <bad_commid_hash_id>
git bisect good <good_commit_hash_id>
# do your test and label the current commit like this
git bisect good/bad
One bonus of bisect tool is it can run on itself if given a script to indecate good and bad. The script should have a correct exit code to tell if the test success. For example,
#!/usr/bin/env bash
mvn clean test
# Save above lines in a script,
# start bisect and label one good and one bad as above steps, and run
git bisect run test_script.sh
Have fun debuging!
comments powered by Disqus