{"id":30841,"date":"2022-07-24T14:39:52","date_gmt":"2022-07-24T14:39:52","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=30841"},"modified":"2022-12-23T05:48:02","modified_gmt":"2022-12-23T05:48:02","slug":"git-tutorial-git-bisect","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/git-tutorial-git-bisect\/","title":{"rendered":"Git Tutorial: Git Bisect"},"content":{"rendered":"\n<p>Find the change that introduced a bug in your code, quickly.<\/p>\n\n\n\n<p>The\u00a0<a href=\"https:\/\/git-scm.com\/docs\/git-bisect\" target=\"_blank\" rel=\"noreferrer noopener\">git bisect<\/a>\u00a0command helps you to find which change introduced a bug in your code. It&#8217;s easy and quick, but most people don&#8217;t know about it.<\/p>\n\n\n\n<p>You notice that in the most recent commit (let&#8217;s say 4a4e), a feature is not working, and you remember that at some point in the past (let&#8217;s say commit f8a4) it was working just fine. The task is to find out which commit introduced the bug.<\/p>\n\n\n\n<p>Git uses the\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Bisection_method\" target=\"_blank\" rel=\"noreferrer noopener\">bisection algorithm<\/a>\u00a0to help you search the offending commit. To start, you need to mark a bad commit and a good commit, git will checkout a commit in the middle for you to test. Then you mark it either as good or bad, and then the process starts again.<\/p>\n\n\n\n<p>We start by identifying a good (900d) and a bad (bad0) commit<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"190\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-1-1024x190.png\" alt=\"\" class=\"wp-image-30842\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-1-1024x190.png 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-1-300x56.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-1-768x143.png 768w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-1.png 1464w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Git will keep the commits that are both descendants of the good commit and ancestors of the bad commit, which will leave us with a smaller graph to work with.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"190\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-2-1024x190.png\" alt=\"\" class=\"wp-image-30843\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-2-1024x190.png 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-2-300x56.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-2-768x143.png 768w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-2.png 1464w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Git chooses a commit between the good and bad commits. As this is a directed acyclic graph, there is no commit &#8220;in the middle&#8221;, git chooses the commit that will provide more information once tested.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"190\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-3-1024x190.png\" alt=\"\" class=\"wp-image-30844\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-3-1024x190.png 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-3-300x56.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-3-768x143.png 768w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-3.png 1464w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Now we need to test this commit as being good or bad. Let&#8217;s say it was bad, we mark it as such and git proposes a new commit to be tested. The graph now is smaller.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"190\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-4-1024x190.png\" alt=\"\" class=\"wp-image-30845\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-4-1024x190.png 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-4-300x56.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-4-768x143.png 768w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-4.png 1464w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>We test this commit as well (let&#8217;s say it was good). In this case, the upper branch is removed from the commits to test, as we are under the assumption that only one commit introduced the bug.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"190\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-5-1024x190.png\" alt=\"\" class=\"wp-image-30846\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-5-1024x190.png 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-5-300x56.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-5-768x143.png 768w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-5.png 1464w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>We test the next commit (it was good) and git proposes the last comit to be tested.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"190\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-6-1024x190.png\" alt=\"\" class=\"wp-image-30847\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-6-1024x190.png 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-6-300x56.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-6-768x143.png 768w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-6.png 1464w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>We test this last commit and we are done, the first bad commit was b3fd.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"190\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-7-1024x190.png\" alt=\"\" class=\"wp-image-30848\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-7-1024x190.png 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-7-300x56.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-7-768x143.png 768w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/git-bisect-7.png 1464w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">To start the process, just invoke git bisect start and mark the commit as bad, and mark a commit that was working f8a4 as good.\n\n(4a4e) $ git bisect start\n(4a4e) $ git bisect bad\n(4a4e) $ git bisect good f8a4\n\nBisecting: 7 revisions left to test after this (roughly 3 steps)\n&#91;cae5] Rename function\nGit will checkout a commit in the middle, you need to test your code to see if this version is working:\n\n(cae5) $ node add.js 1 2 3  \/\/ 6\n(cae5) $  git bisect good\n\nBisecting: 7 revisions left to test after this (roughly 3 steps)\n&#91;0bca] Rename function\nGit will checkout a version in the middle, you test again and mark it as good or bad. After a couple of steps, you will find the offending commit:\n\n\n# 3 or 4 steps later ...\n\n(90d0) $ git bisect bad\n\n5e3c is the first bad commit\ncommit 5e3c\nAuthor: Pablo Navarro Castillo &lt;pnavarrc@gmail.com&gt;  \/\/ &lt;- me :blush:\nDate:   Sat Apr 16 11:02:21 2016 -0300\n\n    read all input arguments\n\n:100755 100755 f111 d7fc M\tadd.js\n\n(5e3c) $ git reset\nNow that we know which commit is the bad one, we can look at the diff to see what changed:\n\n\n$ git show 5e3c5e\n\ncommit 5e3c\nAuthor: Pablo Navarro Castillo &lt;pnavarrc@gmail.com&gt;\nDate:   Sat Apr 16 11:02:21 2016 -0300\n\n    read all input arguments\n\ndiff --git a\/add.js b\/add.js\nindex f1110f6..d7fc703 100755\n--- a\/add.js\n+++ b\/add.js\n@@ -8,7 +8,7 @@ function toInt(num) {\n \/\/ Parse the input arguments\n function readNumbers(input) {\n   var numArgs = input.length;\n-  return input.slice(2, numArgs).map(toInt);\n+  return input.slice(1, numArgs).map(toInt);\n }\n\n \/\/ Read and parse command line arguments\nThe next step is to fix the bug, commit and<\/code><\/span><\/pre>","protected":false},"excerpt":{"rendered":"<p>Find the change that introduced a bug in your code, quickly. The\u00a0git bisect\u00a0command helps you to find which change introduced a bug in your code. It&#8217;s easy and quick, but&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[2],"tags":[],"class_list":["post-30841","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30841","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=30841"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30841\/revisions"}],"predecessor-version":[{"id":30849,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30841\/revisions\/30849"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=30841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=30841"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=30841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}