WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Git · Expert · question 68 of 100

How do you use ’git bisect run’ to automate the process of finding a commit that introduced a bug?

📕 Buy this interview preparation book: 100 Git questions & answers — PDF + EPUB for $5

The git bisect command is a powerful tool used for debugging and identifying the commit that introduced a bug in a Git repository. It works by using a binary search algorithm to identify the "bad" commit that introduced the bug.

In some cases, manually testing each commit can be a time-consuming process. This is where git bisect run can come in handy. With this command, you can automate the testing process and have Git automatically check out each commit and run a script to determine whether it is "good" or "bad".

Here’s an example of how to use git bisect run:

Start by running git bisect start to begin the bisect process. Identify a "bad" commit in your repository by running git bisect bad while on a known bad commit. Identify a "good" commit in your repository by running git bisect good while on a known good commit. Git will automatically check out a commit and prompt you to test it. Instead of manually testing, run a script to automatically determine if the commit is "good" or "bad". You can do this by running git bisect run <test-script> where <test-script> is the path to the script you want to run. Git will start checking out commits and running your test script until it identifies the commit that introduced the bug.

Here’s an example test script that you might use with git bisect run:

    #!/bin/bash
    
    # Build the codebase
    make
    
    # Run the test suite
    make test
    
    # Check the test results
    if grep -q "error" test.log; then
    # The commit is bad
    exit 1
    else
    # The commit is good
    exit 0
    fi

In this example, the script first builds the codebase and then runs the test suite. If any errors are found in the test log, the script exits with a status of 1 (indicating a bad commit). If there are no errors, the script exits with a status of 0 (indicating a good commit).

Using git bisect run can save a lot of time and effort when trying to identify the commit that introduced a bug. However, it’s important to make sure that your test script is reliable and can accurately determine whether a commit is "good" or "bad".

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Git interview — then scores it.
📞 Practice Git — free 15 min
📕 Buy this interview preparation book: 100 Git questions & answers — PDF + EPUB for $5

All 100 Git questions · All topics