The bisect
command is good when you don't know where in your code there is a bug, but you can test for it and thereby find the commit that introduced it. If you already know where in the code the bug is but want to find the commit that introduced it, you can use git blame
. The blame
command will annotate every line in the file with the latest commit that touched that line, making it easy to find the commit ID and then the full context of the commit.
We'll use the same repository and branch as in the bisect example:
$ git clone https://github.com/dvaske/cookbook-tips-tricks.git $ cd cookbook-tips-tricks $ git checkout bug_hunting
We know that the bug is in map.txt
on lines 37-39. To annotate each line in the file with the commit ID and author, we'll run git blame
on the file. We can further limit the search to specific lines with the -L <from>,<to>
option:
From the output, it can be clearly seen that the commit with the ID 83c22a39
by HAL 9000
introduced the bug.
The blame
command can be used even if the file has been refactored and the code has been moved around. With the -M
option, the blame
command can detect lines that have been moved around in the file and with the -C
option, Git can detect lines that were moved or copied from other files in the same commit. If the -C
option is used three times -CCC
, the blame
command will find lines that were copied from other files in any commit.