Git Bisect is useful for debugging something is work is past but not now (Regression).
Example on nodeJS
* 29baf83 (HEAD -> master, origin/master) Update readme
* f9062dc Add /bisect test
* eb3d6e0 Update test to use meanful name
* 9daebf0 Fix testname of /hi
* 808bbdd Add /bisect
* b57d4ba Refactoring hi to controller
* 48ddb04 Refactoring hello to controller
* 7522416 Refactoring /hi
* a12db20 Refactoring / root path
* ff76ce1 Remove /docker
* 81e2c7f Add README
* 1cea276 Init
- You found the last pushed is failed because of the last commit
HEAD
can't startnode app.js
- Your know you can start
node app.js
on previous pushed that on the81e2c7f Add README
commit - Your don't know where commit is the caues of error.
It should betweenff76ce1 Remove /docker
andf9062dc Add /bisect test
- Find the cause of error on the whole code on HEAD commit? Hard work!!!
- Find first commit that make error happen to reduce scope of the cause?
2.
is better way but how many commits that you have to look ?
In this case, There are 10 commits but you can use Bisection method that is alike Binary search algorithm to reduce number of commits. With bisect method, you only look on 4-5 commits that is wortcase of binary search log n + 1
You don't have to calculate where the middle commit
and use git checkout
to it then check status and mark.
You just tell git bisect
where are you (HEAD) and where is the good commit that you know.
git bisect
will checkout the middle commit then you tell it is Good
or Bad
commit, after that move to next one,
The end, You will know where commit is the cause of error.
Prepare dependencise
# Run on docker container
$ docker build -t node-demo-bisect .
$ docker run -it --rm node-demo-bisect sh
# Run on your machine, you should have node v6 and npm
$ npm install
Debug via git bisect
with npm test
script
# On your machine / docker node-demo-bisect
$ git bisect start HEAD 81e2c7f # tell git bisect where to start and where is good commit
$ git bisect run npm test # ues git bisect for automated check the status of commit via test script
$ git show # you found the commit that is caues of error, use `git show` to display how its different from previous commit
$ git bisect reset # end of git bisect, back to HEAD
$ exit # for exit the docker container
You should found the commit b57d4ba Refactoring hi to controller
is cause of error.
# b57d4ba Refactoring hi to controller
var hi = (req, res) => {
res.send('hi')
}
that should fix to be this
# Fix the error
module.exports = (req, res) => {
res.send('hi')
}
- Automated Test especially TDD with FIRST concept.
- Atomic Commit.
Good
- Something that your know it is good. Ex. test passed, compiled without any error, bugless
Bad
- Sometheng that opposite that Good
. Fx. test failed, compile failed, have Bug !!!
$ git bisect start < Bad commit > < Good Commit >
# Bad commit - lastest commit that is bad, normally is HEAD
# Good commit - commit that your know it is good
$ git bisect start < Bad commit > < Good Commit >
$ git bisect bad # when current commit is bad
$ git bisect good # when curret commit is good
$ git bisect start < Bad commit > < Good Commit >
$ git bisect run < script >
- Using Git bisect to figure out when brokenness was introduced
- git bisect - thoughtbot
- Git Bisect: Simple Examples and Automation
- หาบั๊กด้วย git กับคำสั่ง git bisect