0
0
Gitdevops~5 mins

Bisect for finding bug-introducing commits in Git - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes a bug appears in your project, but you don't know which change caused it. Git bisect helps you find the exact commit that introduced the bug by testing commits step-by-step.
When a bug suddenly appears and you want to find which code change caused it.
When you have many commits and manually checking each one is too slow.
When you want to save time by automatically narrowing down the bad commit.
When you want to confirm if a specific commit fixed or caused a problem.
When you want to share the exact bad commit with your team for faster fixes.
Commands
This command starts the bisect process. It tells Git you want to find a bad commit.
Terminal
git bisect start
Expected OutputExpected
No output (command runs silently)
Marks the current commit as bad, meaning it has the bug.
Terminal
git bisect bad
Expected OutputExpected
Bisecting: 4 revisions left to test after this (roughly 2 steps)
Marks the commit tagged 'v1.0' as good, meaning it does not have the bug. This helps Git narrow down the search.
Terminal
git bisect good v1.0
Expected OutputExpected
Bisecting: 2 revisions left to test after this (roughly 1 step)
Runs an automated test script on each commit Git checks during bisect. It marks commits good or bad based on the script's exit code.
Terminal
git bisect run ./test_script.sh
Expected OutputExpected
Bisecting: 1 revision left to test after this (roughly 0 steps) <commit-hash> is the first bad commit
Ends the bisect session and returns you to the original branch and commit.
Terminal
git bisect reset
Expected OutputExpected
Bisect reset to the original HEAD
Key Concept

If you remember nothing else from this pattern, remember: git bisect helps you quickly find the exact commit that introduced a bug by marking known good and bad commits and testing in between.

Common Mistakes
Not marking a known good commit before starting bisect
Git needs a known good commit to compare against the bad commit to narrow down the search.
Always mark a known good commit with 'git bisect good <commit>' after starting bisect.
Forgetting to run 'git bisect reset' after finishing
You remain in bisect mode, which can confuse further work and leave your repo in a detached HEAD state.
Run 'git bisect reset' to return to your original branch and clean up.
Using 'git bisect bad' on a commit that is actually good
This misleads Git and can cause it to find the wrong commit or fail to find the bug.
Carefully test the commit before marking it bad to ensure it really has the bug.
Summary
Start bisect with 'git bisect start' to begin finding the bad commit.
Mark a known bad commit with 'git bisect bad' and a known good commit with 'git bisect good <commit>'.
Use 'git bisect run <script>' to automate testing or manually mark commits good or bad.
Finish with 'git bisect reset' to return to your original branch.