CINXE.COM

Mastering Git Bisect: Finding Bugs with Precision | Juan Vásquez

<!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Mastering Git Bisect: Finding Bugs with Precision | Juan Vásquez</title> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="icon" type="image/svg+xml" href="/favicon.svg"> <!-- Begin Bridgetown SEO tag v6.0.0 --> <title>Mastering Git Bisect: Finding Bugs with Precision | Juan Vásquez</title> <meta property="og:title" content="Mastering Git Bisect: Finding Bugs with Precision" /> <meta name="author" content="Juan Vásquez" /> <meta property="og:locale" content="en" /> <meta name="description" content="Hi! What’s up? I’m Juan Vásquez. In this space you will find articles related to web development mostly using Ruby and JavaScript languages. If you are interested add my blog in your favorite RSS, every time I write something you will receive it. Feel free to contact me on twitter @juanvqz_ when you want." /> <meta property="og:description" content="Hi! What’s up? I’m Juan Vásquez. In this space you will find articles related to web development mostly using Ruby and JavaScript languages. If you are interested add my blog in your favorite RSS, every time I write something you will receive it. Feel free to contact me on twitter @juanvqz_ when you want." /> <link rel="canonical" href="https://juanvasquez.dev/blog/mastering-git-bisect-finding-bugs-with-precision/" /> <meta property="og:url" content="https://juanvasquez.dev/blog/mastering-git-bisect-finding-bugs-with-precision/" /> <meta property="og:site_name" content="Juan Vásquez" /> <meta property="og:type" content="article" /> <meta property="article:published_time" content="2023-09-03T08:00:00-06:00" /> <meta name="twitter:card" content="summary" /> <meta property="twitter:title" content="Mastering Git Bisect: Finding Bugs with Precision" /> <meta name="twitter:site" content="@juanvqz_" /> <meta name="twitter:creator" content="@Juan Vásquez" /> <!-- End Bridgetown SEO tag --> <link type="application/atom+xml" rel="alternate" href="https://juanvasquez.dev/feed.xml" title="Juan Vásquez" /> <link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link href="https://fonts.googleapis.com/css2?family=Rubik:wght@300;400;500&display=swap" rel="stylesheet"> <link rel="stylesheet" href="/_bridgetown/static/index.ZKZ3FOQA.css" /> <script src="/_bridgetown/static/index.WN5PKAGR.js" defer></script> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-MY1NKQYT83"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag("js", new Date()); gtag("config", "G-MY1NKQYT83"); </script> </head> <body class="post"> <header class="header"> <a href="/" class="logo">Juan Vásquez</a> </header> <main class="main"> <div class="article"> <h2 class="text-center">Mastering Git Bisect: Finding Bugs with Precision</h2> <div class="tags"> by Juan Vásquez, 03 Sep 2023 </div> <div class="tags">git</div> <article><h1 id="mastering-git-bisect-finding-bugs-with-precision">Mastering Git Bisect: Finding Bugs with Precision</h1> <p>In the world of software development, bugs are a common adversary. They can be elusive, popping up when you least expect them and wreaking havoc in your codebase. When you encounter a bug, one of the most critical tasks is to identify the exact commit that introduced it. This is where Git Bisect, a powerful tool provided by Git, comes to the rescue.</p> <p>Git Bisect is a tool that helps you perform a binary search through your commit history to find the specific commit responsible for a bug. It automates the process of checking out different commits, allowing you to quickly narrow down the culprit. In this article, we’ll explore how to use Git Bisect effectively.</p> <h2 id="setting-the-stage">Setting the Stage</h2> <p>Imagine you’re working on a project with a large codebase, and you’ve discovered a bug - let’s call it Bug X. You know that at some point in the project’s history, everything was working perfectly. However, due to the complexity of the code and the number of commits, you have no idea when or where Bug X was introduced.</p> <p>This is where Git Bisect shines. It helps you find the problematic commit methodically and efficiently.</p> <h2 id="starting-git-bisect">Starting Git Bisect</h2> <p>The first step is to start the Git Bisect process. Open your terminal and navigate to your project’s repository. Then, run:</p> <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git bisect start </code></pre></div></div> <p>This command initializes the bisect process and prepares Git to help you find the bug’s root cause.</p> <h2 id="marking-good-and-bad-commits">Marking Good and Bad Commits</h2> <p>To begin the search, you need to identify two commits:</p> <ol> <li>A <strong>bad</strong> commit: This is a commit where you know for sure that Bug X exists.</li> <li>A <strong>good</strong> commit: This is a commit where you are certain that Bug X does not exist.</li> </ol> <p>Use the following commands to mark these commits:</p> <ul> <li>To mark a commit as <strong>bad</strong>, use: <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git bisect bad &lt;commit&gt; </code></pre></div> </div> </li> <li>To mark a commit as <strong>good</strong>, use: <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git bisect good &lt;commit&gt; </code></pre></div> </div> </li> </ul> <h2 id="let-the-binary-search-begin">Let the Binary Search Begin</h2> <p>With the good and bad commits marked, Git will now perform a binary search through your commit history. It will check out commits in the middle of the range you specified (between the good and bad commits). You will need to test each of these commits and inform Git if they are “good” or “bad.”</p> <ul> <li>If a checked-out commit is <strong>bad</strong>, run: <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git bisect bad </code></pre></div> </div> </li> <li>If a checked-out commit is <strong>good</strong>, run: <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git bisect good </code></pre></div> </div> </li> </ul> <p>Git will then proceed to check out the next commit in the middle of the remaining range, and you repeat the testing process. Git will continue this cycle until it has identified the exact commit where Bug X was introduced.</p> <h2 id="viewing-bisect-progress">Viewing Bisect Progress</h2> <p>During the bisect process, it’s useful to keep track of the progress. To view the history of the bisect process, you can use:</p> <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git bisect log </code></pre></div></div> <p>This command displays a table with information about the commits that have been checked, including their commit hash, commit message, and whether they were marked as “good” or “bad.”</p> <h2 id="ending-the-bisect-process">Ending the Bisect Process</h2> <p>Once Git has identified the commit that introduced the bug, it will display the commit hash and any additional information. You can end the bisect process by running:</p> <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git bisect reset </code></pre></div></div> <p>At this point, you can investigate the problematic commit, fix the bug, or take any other necessary actions.</p> <h2 id="conclusion">Conclusion</h2> <p>Git Bisect is an invaluable tool in a developer’s toolkit for debugging. It simplifies the process of finding the commit that introduced a bug, saving you time and frustration. Whether you’re working on a large codebase or a small project, Git Bisect can help you pinpoint and resolve issues efficiently, allowing you to deliver more reliable software to your users.</p></article> <p class="article_footer"> Last Update 03 Sep 2023 </p> </div> </main> <footer class="footer"> <div> <a href="https://www.youtube.com/channel/UC9bsDBNvCZEEjrjFdFLtIYA" target="_brank" class="account" aria-label="Link to my youtube" > <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-youtube" width="24"> <path d="M22.54 6.42a2.78 2.78 0 0 0-1.94-2C18.88 4 12 4 12 4s-6.88 0-8.6.46a2.78 2.78 0 0 0-1.94 2A29 29 0 0 0 1 11.75a29 29 0 0 0 .46 5.33A2.78 2.78 0 0 0 3.4 19c1.72.46 8.6.46 8.6.46s6.88 0 8.6-.46a2.78 2.78 0 0 0 1.94-2 29 29 0 0 0 .46-5.25 29 29 0 0 0-.46-5.33z"/> <polygon points="9.75 15.02 15.5 11.75 9.75 8.48 9.75 15.02"/> </svg> </a> <a href="https://twitter.com/juanvqz_" target="_brank" class="account" aria-label="Link to my twitter" > <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-twitter" width="24"> <path d="M23 3a10.9 10.9 0 0 1-3.14 1.53 4.48 4.48 0 0 0-7.86 3v1A10.66 10.66 0 0 1 3 4s-4 9 5 13a11.64 11.64 0 0 1-7 2c9 5 20 0 20-11.5a4.5 4.5 0 0 0-.08-.83A7.72 7.72 0 0 0 23 3z"/> </svg> </a> <a href="https://github.com/JuanVqz" target="_brank" class="account" aria-label="Link to my github" > <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-github" width="24"> <path d="M9 19c-5 1.5-5-2.5-7-3m14 6v-3.87a3.37 3.37 0 0 0-.94-2.61c3.14-.35 6.44-1.54 6.44-7A5.44 5.44 0 0 0 20 4.77 5.07 5.07 0 0 0 19.91 1S18.73.65 16 2.48a13.38 13.38 0 0 0-7 0C6.27.65 5.09 1 5.09 1A5.07 5.07 0 0 0 5 4.77a5.44 5.44 0 0 0-1.5 3.78c0 5.42 3.3 6.61 6.44 7A3.37 3.37 0 0 0 9 18.13V22"/> </svg> </a> <a href="mailto: juan@ombulabs.com" target="_brank" class="account" aria-label="Link to my email" > <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-mail" width="24"> <path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"/> <polyline points="22,6 12,13 2,6"/> </svg> </a> <a href="https://juanvasquez.dev/feed.xml" target="_brank" class="account" aria-label="Link to my rss" > <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-rss" width="24"> <path d="M4 11a9 9 0 0 1 9 9"/> <path d="M4 4a16 16 0 0 1 16 16"/> <circle cx="5" cy="19" r="1"/> </svg> </a> </div> <div> <a href="https://www.bridgetownrb.com" target="_blank"> Kudos to bridgetown.rb for the awesome static site generator! </a> </div> </footer> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10