my-notes

git_bisect

1 min read

git bisect

Binary search to find bug-introducing commit.


πŸ” How Bisect Works

graph LR
    A[Good] --> B[...]
    B --> C[Bad]
    C --> D[Binary Search]
    D --> E[Find Bad Commit]

πŸš€ Start Bisect

Begin Session

git bisect start

Starts bisect session.


Mark Current as Bad

git bisect bad

Marks current commit as having the bug.


Mark Known Good Commit

git bisect good abc1234

Marks a commit where bug didn't exist.


Start with Both

git bisect start HEAD abc1234

Starts with bad (HEAD) and good commit.


πŸ”„ During Bisect

Mark as Good

git bisect good

Current commit doesn't have the bug.


Mark as Bad

git bisect bad

Current commit has the bug.


Skip Current Commit

git bisect skip

Can't test this commit (e.g., doesn't build).


βœ… Finish Bisect

End Session

git bisect reset

Returns to original HEAD and ends bisect.


Reset to Specific Commit

git bisect reset abc1234

Returns to specific commit instead.


πŸ€– Automated Bisect

Run Test Script

git bisect run ./test-script.sh

Automatically tests each commit. Script returns 0 for good, 1 for bad.


Example Test Script

#!/bin/bash
npm test

Simple test script for bisect run.


Run with Command

git bisect run npm test

Uses npm test to determine good/bad.


πŸ“‹ View Bisect Log

Show Log

git bisect log

Shows history of bisect session.


Replay Bisect

git bisect log > bisect.log
git bisect replay bisect.log

Saves and replays bisect session.


πŸ’‘ Example Session

# Start bisect
git bisect start

# Current commit is broken
git bisect bad

# This old commit worked
git bisect good v1.0.0

# Git checks out middle commit
# Test it, then mark:
git bisect good  # or git bisect bad

# Repeat until found
# Git says: "abc123 is the first bad commit"

# End session
git bisect reset

πŸ’‘ Tips

Use Automated Testing

Bisect is most powerful with automated tests.

Visual Bisect
git bisect visualize

Opens visual representation.



#git #bisect #debug #advanced

Built with mdgarden