Git- Debugging

Debugging is an important part of software development, and Git provides powerful tools to help with this process. One such tool is git bisect, which allows developers to efficiently identify the commit that introduced a bug by performing a binary search through the project’s history. This guide will explain how to use git bisect for effective debugging, enhancing your ability to maintain a clean and stable codebase.

What is Git Bisect?

git bisect is a Git command that helps you find the exact commit that introduced a bug or issue. It leverages a binary search algorithm to narrow down the range of commits, making the debugging process faster and more efficient. By marking commits as “good” or “bad,” Git systematically eliminates half of the remaining commits with each step until the problematic commit is identified.

Why Use Git Bisect?

  • Efficiency: Quickly narrows down the problematic commit, saving time compared to manually checking each commit.
  • Precision: Accurately identifies the commit that introduced the bug, making it easier to understand and fix the issue.
  • Automation: Can be automated with scripts for consistent testing, reducing the likelihood of human error.

How it works?

  • git bisect start: It start up the git bisect wizard.
  • git bisect bad “version”: It let the git bisect wizard know of a bad commit.
  • git bisect good “version”: It let the git bisect wizard know of a good commit.

After the following set of commands, git selects the commit in the middle range, checks it and return output.

Note: In this image, we have not mentioned any version of git bisect bad and git bisect good, so when we don’t mention any version, the current version is taken by default.

git bisect options:

  • git bisect reset: After one complete bisect session, git bisect reset command is used to return to original head.
  • To undo the change of state: Sometimes, rather than finding a bug in commit bisect command it can also be used to switch the previous state. So it can be used when we are looking to commit that caused a change between “old” and “new” state. How it works? Instead of bad and good, old and new are used respectively.
    • git bisect start
    • git bisect old “revision”
    • git bisect new “revision”
  • git bisect log: After good and bad revisons, this command is used to get overview of the work done.
  • git bisect skip “version1”..”version2″: It is used to skip a range of commits. It implies no commit ranging from version1 upto version2 should be tested.

git clean:

The git clean command is used to delete untracked files from git project. This command completely removes files from the working tree and cannot be recovered again. So this command should be carefully used.

git clean options:

  • -n or –dry-run This command does not remove files, but it is a dummy command which tells what would be actually done. It is used to test run.
  • -f or –force This command remove files from the current directory. This is a necessary command unless clean.requireforce configuration is set to false.
  • -d This command removes untracked directories along with untracked files.
  • -i or –interactive As the name suggests, it is an interactive command that tells what has to be done.

Interactive Mode:

It is the command loop which displays the available sub commands.

  • clean: This command deletes untracked directories/files at the point.
  • filter by pattern: We can input a pattern such as *_ to exclude files/directories from deletion. For eg: “*.py” will exclude mentioned file extension from deletion.
  • select number: This command allot a number to untracked files/directories to be deleted. We can also make multiple selection. For Example:
    • “3-5” deletes file number “3, 4, 5”.
    • “3-5, 8, 9” deletes “3, 4, 5, 8, 9”.
    • “3-” deletes file numbers starting from 3 to the last file number.
    • “*” deletes everything.
  • ask each: This will ask one by one to delete the particular file or not.
  • quit: This option lets you to exit without deleting.