Git Bisect is a powerful debugging tool that helps you find the commit that introduced a bug by performing a binary search through your project’s commit history.
Git bisect identifies the "bad" commit (the one that introduced the bug) by splitting the range of commits into halves and testing each until the problematic commit is found.
- Efficiently locates the commit causing an issue.
- Automates the debugging process by narrowing down the commit range.
- Bug Identification: When a previously working feature breaks.
- Regression Testing: To pinpoint the commit introducing a regression.
- Collaborative Projects: To debug issues introduced by changes in a team environment.
- Start Bisecting: Mark the current commit as bad (bug present).
- Identify a Good Commit: Specify the last known good commit (bug not present).
- Test Commits: Git checks out a commit in the middle of the range for testing.
- Mark Results: You mark each commit as good or bad based on your tests.
- Locate the Bad Commit: Git narrows the range until the problematic commit is found.
git bisect start
git bisect bad
Identify a commit where the bug wasn’t present:
git bisect good <commit-hash>
Git checks out a commit for you to test. After testing:
- If the bug is present, mark the commit as bad:
git bisect bad
- If the bug is not present, mark the commit as good:
git bisect good
Git continues the process until it identifies the problematic commit. You’ll see output like:
<commit-hash> is the first bad commit
You can automate testing with a script. For example:
- Write a script that returns
0
for good and1
for bad. - Run bisect with the script:
git bisect run ./test-script.sh
Once the bad commit is found, reset your repository to the original state:
git bisect reset
You notice a bug in your project after a recent commit. You know the bug wasn’t present five commits ago.
-
Start bisecting:
git bisect start
-
Mark the current commit as bad:
git bisect bad
-
Mark the last known good commit:
git bisect good HEAD~5
-
Test each commit:
- If the bug is present:
git bisect bad
- If the bug is absent:
git bisect good
- If the bug is present:
-
Git identifies the first bad commit:
<commit-hash> is the first bad commit
-
Reset your repository:
git bisect reset
- Automate Testing: Use scripts to speed up the process for projects with consistent test criteria.
- Document Findings: Record the cause of the issue and steps to resolve it after identifying the bad commit.
- Use Tags or Messages: Use descriptive commit messages or tags to help you identify meaningful points in the history.
Command | Description |
---|---|
git bisect start |
Start the bisect process. |
git bisect bad |
Mark the current commit as bad. |
git bisect good <commit-hash> |
Mark a specific commit as good. |
git bisect run <script> |
Automate testing with a script. |
git bisect reset |
End the bisect process and reset to the original state. |
Yes, as long as the commits in question are part of the same repository history.
You can restart the bisect process using:
git bisect reset
Git bisect is a valuable tool for identifying the source of bugs or regressions efficiently. By combining manual or automated testing with Git’s binary search algorithm, you can debug faster and maintain project stability.
Next Steps: Custom Git Aliases