Git Bisect Tutorial
Overview
The git bisect
command is a powerful tool for identifying the commit that introduced a bug or issue in a Git repository. By performing a binary search through the commit history, git bisect
helps you efficiently locate the specific commit where a problem first appeared.
What is git bisect
git bisect
is a command used to perform a binary search through your commit history to find the commit that introduced a specific bug or issue. The process involves marking commits as good or bad and narrowing down the search until the problematic commit is identified.
Basic Syntax
git bisect start
git bisect bad [commit]
git bisect good [commit]
git bisect run <test-script>
git bisect reset
git bisect start
: Start the bisecting process.git bisect bad [commit]
: Mark the current commit or a specified commit as bad (contains the issue).git bisect good [commit]
: Mark the current commit or a specified commit as good (does not contain the issue).git bisect run <test-script>
: Automatically test each commit with the provided script to identify the bad commit.git bisect reset
: End the bisect session and return to the original branch.
How to Use git bisect
Step 1: Start Bisecting
Begin the bisecting process:
Step 2: Mark the Bad Commit
Specify the commit that is known to have the bug:
Step 3: Mark the Good Commit
Specify a commit that is known to be good (where the bug did not exist):
Step 4: Test Each Commit
Git will checkout a commit between the good and bad commits for you to test. You need to check if the bug exists in this commit and then mark it accordingly:
-
If the bug is present in the commit, mark it as bad:
-
If the bug is not present, mark it as good:
Repeat this process until Git identifies the exact commit that introduced the issue.
Step 5: End Bisect Session
Once the problematic commit is identified, end the bisect session and return to the original branch:
Examples of Using git bisect
Example 1: Basic Manual Bisecting
-
Start Bisecting:
-
Mark Bad Commit:
-
Mark Good Commit:
-
Test Commits:
Test each commit Git checks out and mark as
bad
orgood
. -
End Bisect Session:
Example 2: Automated Bisecting with a Test Script
If you have a test script that can automatically determine if a commit is good or bad, you can use git bisect run
to automate the process.
-
Create a Test Script:
-
Run Bisect with Script:
-
Git will automatically test commits and identify the problematic commit.
Example 3: Finding the Commit That Introduced a Bug
-
Start Bisecting:
-
Mark the Current Commit as Bad:
-
Mark a Known Good Commit:
-
Test and Mark Commits:
Mark each commit as
good
orbad
until the problematic commit is found. -
End Bisect Session:
Common Use Cases
- Debugging: Identifying the commit that introduced a bug or issue in the code.
- Code Review: Finding when a specific feature or change was added.
- Regression Testing: Tracking down when a regression or unwanted change occurred.
Summary
The git bisect
command is a powerful tool for locating the commit that introduced a bug or issue by performing a binary search through the commit history. By marking commits as good or bad and using automated testing scripts, you can efficiently identify problematic changes and resolve issues in your repository.