Git Commands

Debugging with Git

Apart from version control, Git has some generic tools such as File Annotation, Binary Search, and looking for specified patterns in tracked files that can be used for helping with source code debugging.

The git bisect command can be used to find out what commit introduced the bug.

The git blame command can be used to find revision or author last modified each line of code.

git bisect

For example, how would we deal with a situation where a new project release has been pushed to a production environment with bug reports being generated, when there were no issues in the development environment? We can try to review each Production commit sequentially starting with the most recent until we find the commit that generated the Regression error being sought. If there are hundreds of commits, however, this could be a very time-consuming process. An alternative more efficient approach is to debug using the git bisect command.

Description

This command can be used with several sub-commands with different options:

git bisect <subcommand> <options>


git bisect start [--term-{old,good}=<term> --term-{new,bad}=<term>]
	  [--no-checkout] [<bad> [<good>...]] [--] [<paths>...]
git bisect (bad|new|<term-new>) [<rev>]
git bisect (good|old|<term-old>) [<rev>...]
git bisect terms [--term-good | --term-bad]
git bisect skip [(<rev>|<range>)...]
git bisect reset [<commit>]
git bisect (visualize|view)
git bisect replay <logfile>
git bisect log
git bisect run <cmd>...
git bisect help

This command does a binary search to find the specific regression in the project’s commit history that introduced the issue. Using this approach, the broken code can be narrowed down within repositories in minutes. Using a binary search algorithm, this process reduces the number of potentially bad revisions by half with each the git bisect command search execution.


$ git bisect start
$ git bisect bad
$ git bisect good v1.0
Bisecting: 6 revisions left to test after this
[ecb6e1bc347ccecc5f9350d878ce677feb13d3b2] error handling on repo

The git bisect start command used to start up the git bisect wizard.

The git bisect bad command allows the system to know the current commit is broken.

The git bisect good <good_commit> command allows bisect to record when the last known good state was done.

When the debugging process has been completed and the bad commit with broken code has been found, the git bisect reset command is used to terminate the git bisect wizard to reset HEAD to its original state:

git bisect reset <commit> 

The git bisect command can also be automated by running a script:


$ git bisect start HEAD v1.0
$ git bisect run test-error.sh

git blame

This command only operates on individual files. It examines their content line by line, outputting when each line was last modified and the author of each modification. The output format can be changed by using different options, and a file-path to which the output can be directed must be provided.

Examples

$ git blame -L 1,7 README.MD

The -L <start>,<end> option restricts the output to the requested line range. In the above example, the output is restricted to lines 1 through 7 in the README.MD file.

$ git blame -e README.MD

The -e option shows an output with the authors email address instead of username in the README.MD file.

$ git blame -C README.MD

The -C option detects lines moved or copied from other files, and can very useful when restructuring and moving code across files. The output shows the original author of the lines as opposed to the last author who moved or copied those lines.

$ git blame -M README.MD

The –M option detects moved or copied lines within the same file. This output also shows the original author of the lines instead of the last author who moved or copied the lines.

Conclusion

Knowledge of git tools can help developers save valuable time when working on large projects with hundreds of commits added by several developers within a short period.

This is a link to Documentation providing more information about debugging with Git.

Links to relevant YouTube tutorials are provided below:
Link 1
Link 2


Leave a comment