bisect
The git bisect command is used to find the commit that introduced a bug by performing a binary search through the commit history. This is particularly useful for large repositories with many commits.
Detailed Explanation
Starting Bisect: You start the bisect process by specifying a known good commit and a known bad commit. Git will then check out a commit in the middle of the range.
Marking Commits: You mark each commit as good or bad using
git bisect goodandgit bisect bad. Based on your input, Git will narrow down the range of commits to find the exact commit that introduced the bug.Automating Bisect: You can automate the bisect process by providing a script that tests for the bug. Git will run the script at each step and mark the commit as good or bad based on the script's exit status.
Ending Bisect: Once the offending commit is found, you end the bisect process using
git bisect reset, which returns your repository to its original state.
Examples
Starting Bisect:
git bisect start git bisect bad HEAD git bisect good v1.0This starts the bisect process with the current commit marked as bad and the
v1.0tag marked as good.Marking a Commit as Good:
git bisect goodThis marks the current commit as good, indicating that the bug was not present in this commit.
Marking a Commit as Bad:
git bisect badThis marks the current commit as bad, indicating that the bug is present in this commit.
Automating Bisect with a Script:
git bisect start git bisect bad HEAD git bisect good v1.0 git bisect run ./test-script.shThis starts the bisect process and uses
./test-script.shto automatically test each commit. The script should exit with status 0 if the commit is good and non-zero if the commit is bad.
Common use cases
Common use cases for the git bisect command include:
Identifying the Commit that Introduced a Bug: When a bug is discovered,
git bisectcan help pinpoint the exact commit that introduced the bug by performing a binary search through the commit history.Finding Performance Regressions: If a performance issue is detected,
git bisectcan be used to find the commit that caused the regression by testing the performance at each step.Locating the Source of a Test Failure: When a test starts failing,
git bisectcan help identify the commit that caused the test to fail, making it easier to understand and fix the issue.Debugging Complex Codebases: In large repositories with many commits,
git bisectcan efficiently narrow down the range of commits to find the one responsible for a problem, saving time and effort compared to manually checking each commit.