📖 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
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
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
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
Hint
The output should be the commit hash of the first bad commit found.
Practice
(1/5)
1. What is the main purpose of git bisect run in debugging?
easy
A. To manually mark commits as good or bad during bisect
B. To automate the process of finding the first bad commit using a test script
C. To reset the repository to the latest commit
D. To merge two branches automatically
Solution
Step 1: Understand the role of git bisect run
This command automates the bisect process by running a test script on each commit to check if it is good or bad.
Step 2: Compare with other options
Manual marking is done without run, resetting or merging are unrelated to bisect automation.
Final Answer:
To automate the process of finding the first bad commit using a test script -> Option B
Quick Check:
Automation with test script = B [OK]
Hint: Remember: git bisect run automates testing commits [OK]
Common Mistakes:
Confusing manual bisect with automated bisect
Thinking it resets or merges branches
Assuming it marks commits without a script
2. Which of the following is the correct syntax to start an automated bisect with a test script named test.sh?
easy
A. git bisect start test.sh
B. git bisect automate test.sh
C. git bisect run ./test.sh
D. git bisect test ./test.sh
Solution
Step 1: Recall the correct command structure
The correct syntax to run a test script during bisect is git bisect run <script>.
Step 2: Match options with syntax
Only git bisect run ./test.sh matches the correct syntax with the script path ./test.sh.
Final Answer:
git bisect run ./test.sh -> Option C
Quick Check:
Correct syntax = D [OK]
Hint: Use git bisect run <script> to automate tests [OK]
Common Mistakes:
Using 'start' instead of 'run'
Omitting './' before script name
Using incorrect verbs like 'automate' or 'test'
3. Given the following test script test.sh that exits with 1 if a file bug.txt exists, else exits 0:
#!/bin/sh
if [ -f bug.txt ]; then
exit 1
else
exit 0
fi
What will git bisect run ./test.sh interpret if bug.txt is present in a commit?
medium
A. The commit will be skipped
B. The commit is good
C. The bisect will stop with an error
D. The commit is bad
Solution
Step 1: Understand exit codes in test script
The script exits with 1 if bug.txt exists, which means failure (bad commit).
Step 2: Interpret exit code in bisect context
git bisect run treats exit code 0 as good and non-zero as bad, so exit 1 means bad commit.
Final Answer:
The commit is bad -> Option D
Quick Check:
Exit 1 = bad commit = A [OK]
Hint: Exit 0 means good; non-zero means bad in bisect scripts [OK]
Common Mistakes:
Confusing exit 1 as good
Assuming script errors stop bisect
Thinking bisect skips commits on exit 1
4. You run git bisect run ./test.sh but it stops with the error: ./test.sh: Permission denied. What is the most likely cause?
medium
A. The test script is missing execute permission
B. The bisect command is misspelled
C. The repository is not initialized
D. The test script returns exit code 0
Solution
Step 1: Analyze the error message
The error Permission denied means the shell cannot run the script, often due to missing execute permission.
Step 2: Check other options
Bisect command spelling or repo initialization would cause different errors; exit code 0 does not cause this error.
Final Answer:
The test script is missing execute permission -> Option A
Quick Check:
Missing execute permission = A [OK]
Hint: Make script executable with chmod +x before running [OK]
Common Mistakes:
Ignoring file permissions
Assuming bisect command typo
Thinking exit code causes command not found
5. You want to find the first bad commit that introduced a bug detected by a Python test script check_bug.py. The script returns exit code 0 if no bug and 1 if bug found. Which sequence of commands correctly automates bisecting?
hard
A. git bisect start; git bisect bad <bad_commit>; git bisect good <good_commit>; git bisect run python3 check_bug.py
B. git bisect start; git bisect good; git bisect bad <bad_commit>; git bisect run python3 check_bug.py
C. git bisect start; git bisect bad; git bisect good <good_commit>; git bisect run python3 check_bug.py
D. git bisect start; git bisect good <bad_commit>; git bisect bad <good_commit>; git bisect run python3 check_bug.py
Solution
Step 1: Understand bisect start sequence
You must start bisect, mark a known bad commit first, then a known good commit, then run the test script.
Step 2: Match the correct order
git bisect start; git bisect bad <bad_commit>; git bisect good <good_commit>; git bisect run python3 check_bug.py correctly marks bad commit first, then good commit, then runs the script with Python.
Final Answer:
git bisect start; git bisect bad <bad_commit>; git bisect good <good_commit>; git bisect run python3 check_bug.py -> Option A
Quick Check:
Bad then good then run script = C [OK]
Hint: Always mark bad commit first, then good, then run script [OK]
Common Mistakes:
Swapping good and bad commit order
Forgetting to start bisect
Running script before marking commits
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.