Tracking Bugs in Git Codebase

GitGitBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Git is a powerful version control system that allows developers to track changes to their codebase over time. One of the most useful features of Git is the ability to find the commit that introduced a bug. This can be incredibly helpful when trying to track down the source of a problem in your code.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("`Git`")) -.-> git/BasicOperationsGroup(["`Basic Operations`"]) git/BasicOperationsGroup -.-> git/commit("`Create Commit`") subgraph Lab Skills git/commit -.-> lab-12739{{"`Tracking Bugs in Git Codebase`"}} end

Manually Find the Commit that Introduced a Bug

Your task is to manually find the commit that introduced a bug in the git-playground repository. The repository can be found at https://github.com/labex-labs/git-playground. The bug is that the file2.txt file should print "This is file2.txt." instead of "This is file2.".

To complete this lab, you will need to use the git bisect command to perform a binary search through the commit history of the repository. You will need to mark commits as either "good" (bug-free) or "bad" (buggy) until you have narrowed down the commit that introduced the bug.

  1. Change into the repository directory:
cd git-playground
  1. Start the git bisect process:
git bisect start
  1. Mark the current commit as "bad":
git bisect bad HEAD
  1. Mark a commit with the message "Initial commit" as "good". Git will automatically checkout a new commit for you to test:
git bisect good 3050fc0de

Git will automatically checkout a new commit for you to test. 5. If the contents of the checked file2.txt file do not match the bug, mark it as "good":

cat file2.txt
git bisect good
  1. If the contents of the checked file2.txt file match the bug, mark it as "bad":
git bisect bad
  1. Once you have found the buggy commit, reset the git bisect process:
git bisect reset

You can now examine the code changes in the buggy commit to find the source of the bug.

This is the result of the test:

d22f46ba8c2d4e07d773c5126e9c803933eb5898 is the first bad commit
commit d22f46ba8c2d4e07d773c5126e9c803933eb5898
Author: Hang <[email protected]>
Date:   Wed Apr 26 14:16:25 2023 +0800

    Added file2.txt

 file2.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 file2.txt

Summary

In this lab, you learned how to use the git bisect command to manually find the commit that introduced a bug in a Git repository. By marking commits as either "good" or "bad", you can perform a binary search through the commit history to quickly narrow down the source of the problem. This technique can be incredibly helpful when debugging complex codebases.

Other Git Tutorials you may like