Debugging with Git

When you are working on a huge project, you may discover bugs in the code that prevent you from proceeding any further in your development. How to fix them?

Thank you, Git has multiple tools that can help you hunt for a bug or the culprit when things go wrong.

Git Blame

$ git blame <file_path/file_name>At The the Command Helps you that the commit the Find at The specific Line of the Created at The Causes A bug in code that A specific File of A Project. It Also at The Determines at The author of the commit, Making IS Easier to ASK for More Information at The code the About.git blame

You can -L option to limit the line output range.

$ git blame -L 11,21 new_file
^95d69a196b5c7 (Jhon Smith  2018-05-18 13:04:22 +0200 11) def new
^95d69a196b5c7 (Jhon Smith  2018-05-18 13:04:22 +0200 12) @article = Article.new
^95d69a196b5c7 (Jhon Smith  2018-05-18 13:04:22 +0200 13) end
3171aa2dbbce7 (David Smith 2018-05-16 18:21:30 +0200 14) def edit
3171aa2dbbce7 (David Smith 2018-05-16 18:21:30 +0200 15) @article = Article.find(params[:id])
3171aa2dbbce7 (David Smith 2018-05-16 18:21:30 +0200 16) end
^95d69a196b5c7 (Jhon Smith  2018-05-18 13:04:22 +0200 17) def create
3171aa2dbbce7 (David Smith 2018-05-16 18:21:30 +0200 18) @article = Article.new(article_params)
^95d69a196b5c7 (Jhon Smith  2018-05-18 13:04:22 +0200 19) if @article.save
^95d69a196b5c7 (Jhon Smith  2018-05-18 13:04:22 +0200 20) redirect_to @article
^95d69a196b5c7 (Jhon Smith  2018-05-18 13:04:22 +0200 21) else

The following by partial-the SHA. 1 of the commit you A CAN Easily See WHO, When and How WAS A specific Line of Modified code. Note that The ^prefix Shows that were Lines Created in the commit and have have remained The Initial Operating since Unchanged of Ever.

Use -Coption to figure out where snippets of code originally came from if they were copied from elsewhere. It tells you the original author and commits regardless of the refactoring done afterward.

$ git blame -L -C 11,21 <file_path/file_name>

git blameIs help when you can assume the cause of the problem. What if you had no idea how to get back to a working state? This is where where git bisectcomes into play.

Git Bisect

A git bisectis a debugging tool used to find out which specific commit introduced a bug or a problem in the project by doing an automatic binary search . You don't know what file in the project contains the bug.

If you don't know what is breaking, and there has been a bunch of commits since the last state where you know the code worked, you'll likely turn to git bisectfor help.

What git bisectdoes is, it divides the git commit tree into "good", bug-free commits and "bad" commits by testing them with binary search . Based on the result of the tests, Git navigates through recent commits opposed them, them up until it finds The culprit. This is known as the binary search algorithm .

If you have multiple bugs, you need to perform a binary search for each of the bugs.

How Does This Work?

  1. First, let's start with the binary search mode to find a bug: $ git bisect start.
  2. Next, you need to look for a commit where everything was still working. To do so, let's examine the commit history to find what you need: $ git log --oneline.

--oneline option shows only the names of git commits.

$ git log --oneline
f11c599 Removed unnecessary lines
95d69a1 Added article tests
3171aa2 Enabled editing articles
95d69a1 Added articles
  1. Tag the oldest "good" commit SHA-1: $ git bisect good 95d69a1.
  2. After you have assigned the "good" tag, you need to find a "bad" commit to divide the commit tree where Git can apply the binary search algorithm. Since you know that the latest commit has the error, you will assign it as the "bad" commit: $ git bisect bad f11c599.
  3. Once you have assigned initial and final pointers for your search, Git walks you through the commit history and tags "good" and "bad" commits.
  4. This process continues until you successfully find out the first "bad" commit, the cause of your problem. Now you can exit the git binary search mode by executing: $ git bisect reset.

Git Grep

$ git grep <keyword>The command allows you to efficiently and quickly search through your project for a string or regular expression in any of the files in your source code. ==It avoids searching through files.==git grep .gitignore

GREP stands for G lobal R egular E xpression P rint.

Additional options:

  • -nOr --line-number: Prints out the line numbers where Git has found matches.
  • -iOr --ignore-case: Ignores case differences between the searched keyword and the file.
  • -cOr --count: Shows the number of matches found in the file for the divided keyword.
  • -pOr --show-function: Displays the context of the searched keyword.
  • --and: Ensures multiple matches in the same line of text.

Summary

git blameIs a great tool if you know where the buggy code is located. On the other hand, if your repository is large large, with a huge commit history that makes it difficult to find the error, git bisectis the way to go. Or you could easily Search through your project for a string or regular expression with git grep.

Three debugging tools with three different ways to fix your problems in their own unique ways. Which one did you encounter so far? Share your experience!

This article is originally published at Kolosek Blog.

转载于:https://my.oschina.net/u/3772078/blog/1831672

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值