0
0
Gitdevops~30 mins

git bisect run for automated bisect - Mini Project: Build & Apply

Choose your learning style9 modes available
Automate Bug Finding with git bisect run
📖 Scenario: You are working on a software project and a bug was introduced somewhere in the recent commits. You want to find the exact commit that caused the bug automatically using git bisect run.This tool helps you run a test script on each commit during the bisect process, so you don't have to manually check each commit.
🎯 Goal: Build a simple automated bisect process using git bisect run with a test script that checks if the bug is present.
📋 What You'll Learn
Create a test script file named test_bug.sh that exits with 0 if the bug is fixed and 1 if the bug is present
Start git bisect with a known good and bad commit
Run git bisect run ./test_bug.sh to automate the bisect
Print the final bad commit found by git bisect
💡 Why This Matters
🌍 Real World
Automated bisect helps developers quickly find the exact commit that introduced a bug without manually checking each commit.
💼 Career
Knowing how to use <code>git bisect run</code> is valuable for debugging and maintaining code quality in software development jobs.
Progress0 / 4 steps
1
Create a test script to detect the bug
Create a file named test_bug.sh with the following content exactly:
#!/bin/sh
if grep -q 'BUG' file.txt; then exit 1; else exit 0; fi
This script checks if the word 'BUG' is in file.txt. If yes, it exits 1 (bad), else 0 (good).
Git
Need a hint?

Use grep -q 'BUG' file.txt to check for the bug word silently.

2
Start git bisect with known good and bad commits
Run git bisect start to begin bisecting.
Then run git bisect bad to mark the current commit as bad.
Finally, run git bisect good <commit-hash> to mark a known good commit. Use the exact commands in this order.
Git
Need a hint?

Replace <commit-hash> with the actual known good commit hash.

3
Run automated bisect with the test script
Run git bisect run ./test_bug.sh to let Git automatically test each commit using your script.
Git
Need a hint?

This command runs your script on each commit to find the first bad one.

4
Print the final bad commit found
After the bisect finishes, run git bisect log to see the bisect history.
Then run git bisect reset to end bisect mode.
Finally, print the commit hash of the first bad commit found using git rev-parse HEAD.
Git
Need a hint?

The output should be the commit hash of the first bad commit found.