Make delicious recipes!

Debugging with git

Use git blame to see when each line of the method was last edited and by whom.
Use -L option to limit the output to specified lines.
git blame -L 5,20 foo.txt

Binary search through commits.
Git provides a binary search command to help you find a misbehaving commit.
Suppose, there have been 16 commits in the last two days and a bug was introduced in one of these commits.
An obvious methodology to find the misbehaving commit is to use binary search.

A sample run for examining commits is as follows:
check 8th commit, PASS (bug not seen)
check 12th commit, FAIL (bug seen)
check 10th commit, PASS (bug not seen)
check 11th commit, PASS (bug not seen)
=> bug was introduced in the 12th commit

Manually, you would do this by calculating which commit to check out, then switching to that commit, checking test-status and so on.
With git, this can be automated using git bisect command.

Following commands show how the above can be made easy using git:
git bisect start
git bisect bad (tells git that current version is broken)
git bisect good v1 (tells git that good version known to you is v1)
// At this point, git knows good and bad versions and sets HEAD to the middle of these two versions
// run your test on this version
git bisect good (tells git that test passed and current version set by git has no issues)
// Now also, git knows good and bad versions, and so moves HEAD to 12th commit
// run your test again
git bisect bad (because our test fails on 12th commit, as shown above)
// git sets HEAD to 10th commit
git bisect good
// HEAD set to 11th commit
git bisect good
// HEAD set to 12th commit, which is the buggy version
git bisect reset (moves the HEAD to the state from where you started)

Now imagine putting the above in a script which runs the testcase and issues git bisect good/bad based on the testcase's output.
You have an automated way of finding the bad commit!!

Git goes an extra mile by allowing you to specify a way of running your test.
git bisect run [command-with-args]

Like us on Facebook to remain in touch
with the latest in technology and tutorials!

Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Email: (Your email is not shared with anybody)

Facebook comments:

Site Owner: Sachin Goyal