Sobes.tech
Junior — Middle

Is there a Git command that can identify the specific commit that caused an error or failure?

sobes.tech AI

Answer from AI

Yes, Git has a command called git bisect that helps find the specific commit that caused an error or failure.

How it works:

  1. You specify two commits in Git — a known good one (without the error) and a known bad one (with the error).
  2. Git automatically performs a binary search between these commits, suggesting to check the state of the code in intermediate commits.
  3. You mark each checked commit as "good" or "bad".
  4. Ultimately, Git finds the exact commit where the error appeared.

Example of usage:

git bisect start
git bisect bad           # current commit with error
git bisect good v1.0     # last known working commit
# Git switches to an intermediate commit
# Check if the error exists
# If error exists:
git bisect bad
# If no error:
git bisect good
# After several iterations, Git will show the problematic commit

# To finish:
git bisect reset

This is a very useful tool for quickly localizing problems in the change history.