git bisect is a Git command that helps you efficiently identify the commit where a bug was first introduced.

Imagine you are working on a project, and suddenly a bug appears.

To fix it properly, You need to figure out which commit caused the bug.

A common manual approach is to check out previous commits one by one, running your code at each step to see if the bug still occurs.

This process is time-consuming and error-prone.

git bisect automates this by using a binary search strategy, allowing you to find the faulty commit much faster.

How it works

Let’s say you discovered the bug is present in commit dcfecf7, but you’re sure it didn’t exist in commit a4fcb9b.

1. Start Bisect

Biegin the process with:

git bisect start

This puts Git into bisect mode, ready to perform a binary search.

2. Mark Good and Bad Commit

Next, You need to label the known good and bad commit.

git bisect good
git bisect bad

These commands apply to the current HEAD, or you can pass in a specific commit ID:

git bisect good dcfecf7
git bisect bad a4fcb9b

Git requires both a good and a bad commit to calculate the range for its search.

To check what you’ve already marked, run:

git bisect log

❗ If you accidentally marked a wrong commit and want to restart, simply run git bisect reset

In my case:

git bisect bad dcfecf7
git bisect good a4fcb9b

Git will now move HEAD to the midpoint of the range using binary search (e.g., to commit 3f85672 )

3. Check the Commit and Continue

Now, check whether the current commit still cause the bug.

If the bug still exists, mark the commit as bad:

git bisect bad

If the bug is gone, mark it as good:

git bisect good

Then, Git will keep moving HEAD closer to the faulty commit.

Marking the current commit as bad moves you to 4c4c904 .

Marking the 4c4c904 as good moves you to cb43c2e .

Repeat this until Git pinpoints the first bad commit.

Once it finds it, you’ll see a message like this:

4. Finish the Bisect Session

After identifying the faulty commit, exit bisect mode with:

git bisect reset

This will restore your HEAD to where it was before the bisect started.

댓글남기기