Module 03 · Lesson 3.3
Using History and Bisection
On this page
- What you will learn
- Why version history helps you debug
- Worked scenario: the test everybody ignored
- Step-by-step workflow: search, bisect, read
- The real output, step by step
- What the culprit commit does and does not tell you
- Using an AI assistant with history
- Conclusion and your next step
- Reusable prompt
- Expected output
- Common errors and fixes
- Validation checklist
- Hands-on exercise
- Knowledge check
- Frequently asked questions
- References
Suppose a recipe book used to give a perfect cake, and now the cake is sour. Someone changed a line. You could read every page, or you could open the book in the middle, bake that version and ask: sour or not? Then you halve the pile again. In a few bakes you find the page.
That is git bisect. Git is the tool that records every change to a project. Bisect uses those records to find the one change that broke something, in a handful of steps. In this lesson you also learn to search history for a piece of code, and to read the change you find. The example continues the napkin bug from Tidewell Wholesale, a made-up supplier, with a simplified copy of its code.
What you will learn
- Use
git logandgit log -Sto search history for a change. - Write a test that git can use as a judge of good and bad commits.
- Run
git bisect runto locate the commit where a defect first appeared. - Read the culprit commit with care, and separate the change from the reason for it.
Before you start
- Finish Hypotheses and Experiments. You should have a supported hypothesis, but not yet know when it began.
- Git installed, and a repository with real history. The commands work in Git Bash on Windows.
- A test, or a short script, that fails when the defect is present and passes when it is not.
Why version history helps you debug
Most bugs in old code are regressions. A regression is something that used to work and then stopped. If it used to work, then at some moment a change broke it. Finding that moment is often faster than reading the code.
- Commit
- One saved change in git, with a message, a date and an author. It is like one numbered page of a diary.
- Good commit and bad commit
- A good commit is a version where the behaviour is right. A bad commit is a version where it is wrong. Bisection needs one of each.
- git bisect
- A git command that checks out the commit halfway between a good one and a bad one, and asks you whether it is good or bad. It repeats until it finds the first bad commit.
- Pickaxe search
git log -S "text"lists commits where the number of times that text appears in the code changed. It is like asking, 'When did this word first appear in the book?'- Exit code
- A number a program returns when it finishes. By custom, 0 means success and anything else means failure. Bisect uses it as the judge.
History tells you what changed and when. It does not tell you why. A commit message is a clue about the intent, but the person may have been wrong, rushed or missing facts. Treat it as a lead, as you did in Module 2.
Worked scenario: the test everybody ignored
Before you start, you run the project's tests to see what the baseline looks like. A baseline is the state of the tests before you change anything. One test fails. You look at it.
from decimal import Decimal
from legacy.pricing_v1 import contract_unit_price
# FLAKY: fails on some machines. Ignore until someone has time.
def test_contract_price_rounding():
assert contract_unit_price("1.25", 10) == Decimal("1.13")As it stands on the main branch, before any fix.
The comment says the test is 'flaky'. A flaky test is a test that sometimes passes and sometimes fails for no clear reason. People learn to ignore them. But look at what this test checks: a contract price of 1.25 with a 10% discount should be 1.13. That is exactly your bug.
F..... [100%]
================================== FAILURES ===================================
________________________ test_contract_price_rounding _________________________
def test_contract_price_rounding():
> assert contract_unit_price("1.25", 10) == Decimal("1.13")
E AssertionError: assert 1.12 == Decimal('1.13')
E + where 1.12 = contract_unit_price('1.25', 10)
E + and Decimal('1.13') = Decimal('1.13')
tests\test_contract_pricing.py:8: AssertionError
=========================== short test summary info ===========================
FAILED tests/test_contract_pricing.py::test_contract_price_rounding - Asserti...
1 failed, 5 passed in 0.08sIt is not flaky. It fails every time, and it fails for the very reason on your reproduction card. The label hid a real signal. This is why you record a baseline: an old failing test is evidence, not noise.
You now have what bisection needs. You have a judge, which is this test. You need a bad commit, which is the latest one. And you need a good commit. A release tag, v1.0, marks a version that the team trusted, and the test existed then.
Step-by-step workflow: search, bisect, read
Step 1: Read the recent history first
Look at the list of commits. Sometimes a message tells you where to look, and it costs ten seconds.
Bash git log --onelineWhat you should see: One line per commit, newest first. See the output below.
Step 2: Search history for the suspect code
Your experiments pointed at the built-in
round(function. Ask git which commits changed the number of times that text appears.Bash git log -S "round(" --onelineWhat you should see: A short list. In this practice repository, a single commit.
Step 3: Choose a judge, a good commit and a bad commit
The judge is a command that exits with 0 when the behaviour is right and with a non-zero number when it is wrong. Here it is one test. The bad commit is where you are now. The good commit is the tag
v1.0.What you should see: You can say: 'It passed at v1.0 and fails now.'
Step 4: Start the bisection and let git run the judge
Give git the bad commit first and the good one second.
git bisect runthen checks out commits for you, runs the command, and reads its exit code each time.Bash git bisect start HEAD v1.0 git bisect run python -m pytest -q --tb=no tests/test_contract_pricing.py::test_contract_price_roundingWhat you should see: Git prints each step, then 'is the first bad commit'.
Step 5: Read the culprit commit
Print the change and read what it did to the code, line by line. Then check that it matches your hypothesis.
Bash git show 4ad78b0What you should see: The full diff, with its message and date.
Step 6: Leave bisect mode
Always finish with
git bisect reset. It puts you back on the branch you started from.Bash git bisect resetWhat you should see: 'Switched to branch main'.
- Good and bad
v1.0 passes, HEAD fails
- Pick the middle
Check out that commit
- Run the judge
Exit code 0 or not
- Halve the range
Keep the half with the change
- Repeat
A few steps in all
- First bad commit
Read it
Each test halves the number of suspects.
The real output, step by step
Everything below came from a small practice repository with ten commits and fixed dates, so you can see exactly what git printed. The author and email are invented.
The history and the pickaxe search
01d9417 Describe price bands in README
b940624 Document price_order arguments
045ffbc Mark test_contract_price_rounding as flaky
bc8bdcb Add test for the volume discount boundary
4ad78b0 Simplify contract pricing (drop Decimal)
72d4982 Add README note on running tests
703e52c Reject negative quantities
1ab5c20 Use contract pricing for the Contract band
e7be3cf Add contract pricing with Decimal rounding
cb26c80 Add price_order for Standard and Volume bands4ad78b0 Simplify contract pricing (drop Decimal)The search found one commit: 'Simplify contract pricing (drop Decimal)'. The word round( appears in the code for the first time there. The message already hints at the cause. But hints are not proof, so you still bisect.
The bisection
Bisecting: 3 revisions left to test after this (roughly 2 steps)
[4ad78b0d981f85be1594966549a174f1d7238502] Simplify contract pricing (drop Decimal)
running 'python' '-m' 'pytest' '-q' '--tb=no' 'tests/test_contract_pricing.py::test_contract_price_rounding'
F [100%]
=========================== short test summary info ===========================
FAILED tests/test_contract_pricing.py::test_contract_price_rounding - Asserti...
1 failed in 0.03s
Bisecting: 1 revision left to test after this (roughly 1 step)
[703e52c55c603c727ae26bcbf38a5beb83405af0] Reject negative quantities
running 'python' '-m' 'pytest' '-q' '--tb=no' 'tests/test_contract_pricing.py::test_contract_price_rounding'
. [100%]
1 passed in 0.03s
Bisecting: 0 revisions left to test after this (roughly 0 steps)
[72d4982e95de26150d2a07d3f3f923b5ea80596d] Add README note on running tests
running 'python' '-m' 'pytest' '-q' '--tb=no' 'tests/test_contract_pricing.py::test_contract_price_rounding'
. [100%]
1 passed in 0.02s
4ad78b0d981f85be1594966549a174f1d7238502 is the first bad commit
commit 4ad78b0d981f85be1594966549a174f1d7238502
Author: Tidewell Dev <dev@example.com>
Date: Mon Jun 2 10:00:00 2025 +0000
Simplify contract pricing (drop Decimal)
invoicing/totals.py | 2 +-
legacy/pricing_v1.py | 12 ++++--------
2 files changed, 5 insertions(+), 9 deletions(-)
bisect found first bad commit- '3 revisions left to test' means three commits are still unchecked after the first one that git chose.
- Each 'running ...' line shows the judge being run on a commit. 'F' and '1 failed' mean the test failed, so git marked it bad. A dot and '1 passed' mean good.
- The first commit git tried failed. So the change is at or before it.
- The two earlier commits passed. So the first bad commit is the one git named.
- In the end git needed three test runs, for eight commits between the good and the bad one. The count grows slowly. A thousand commits need about ten.
Reading the culprit commit
commit 4ad78b0d981f85be1594966549a174f1d7238502
Author: Tidewell Dev <dev@example.com>
Date: Mon Jun 2 10:00:00 2025 +0000
Simplify contract pricing (drop Decimal)
diff --git a/invoicing/totals.py b/invoicing/totals.py
index a7c8375..63d3f63 100644
--- a/invoicing/totals.py
+++ b/invoicing/totals.py
@@ -16,7 +16,7 @@ def price_order(lines, band, prices, contract_discount=0):
raise ValueError("quantity cannot be negative")
unit = prices[sku]
if band == "Contract":
- unit = contract_unit_price(unit, contract_discount)
+ unit = Decimal(str(contract_unit_price(unit, contract_discount)))
elif band == "Volume" and qty >= VOLUME_QTY:
unit = unit * VOLUME_FACTOR
goods += (unit * qty).quantize(PENNY, rounding=ROUND_HALF_UP)
diff --git a/legacy/pricing_v1.py b/legacy/pricing_v1.py
index cb3a3bb..b72ca03 100644
--- a/legacy/pricing_v1.py
+++ b/legacy/pricing_v1.py
@@ -1,12 +1,8 @@
"""Contract pricing, kept from the original pricing service."""
-from decimal import Decimal, ROUND_HALF_UP
-
-PENNY = Decimal("0.01")
def contract_unit_price(list_price, discount_percent):
- """Unit price for a contract customer, rounded half up to the penny."""
- price = Decimal(str(list_price))
- discount = Decimal(str(discount_percent))
- unit = price * (Decimal(100) - discount) / Decimal(100)
- return unit.quantize(PENNY, rounding=ROUND_HALF_UP)
+ """Unit price for a contract customer, rounded to the penny."""
+ price = float(list_price)
+ unit = price * (1 - discount_percent / 100)
+ return round(unit, 2)- Lines that start with
-were removed. Lines that start with+were added. - The
Decimalimport, thePENNYconstant and the exact sum were removed. - A
floatand the built-inround(unit, 2)replaced them. This matches the supported hypothesis exactly. price_orderin the other file was changed to turn the float back into aDecimal. That is why the other tests kept passing.
Another useful command is git blame. It shows, for each line of a file, the commit that last changed it. It is like looking at who last wrote on each line of a shared page.
git blame -L 4,8 --date=short legacy/pricing_v1.pye7be3cf5 (Tidewell Dev 2025-03-17 4) def contract_unit_price(list_price, discount_percent):
4ad78b0d (Tidewell Dev 2025-06-02 5) """Unit price for a contract customer, rounded to the penny."""
4ad78b0d (Tidewell Dev 2025-06-02 6) price = float(list_price)
4ad78b0d (Tidewell Dev 2025-06-02 7) unit = price * (1 - discount_percent / 100)
4ad78b0d (Tidewell Dev 2025-06-02 8) return round(unit, 2)The four lines that matter all come from the same commit. The name in the output is the invented 'Tidewell Dev'. Use the output to find the commit and read its message, not to find someone to blame.
What the culprit commit does and does not tell you
You found where the behaviour changed. You still have three more questions.
| Question | What history can tell you | What it cannot tell you |
|---|---|---|
| What changed? | The exact lines, from the diff. | Nothing is missing here. |
| When did it change? | The date and the order of commits. | When it reached customers. That needs release records. |
| Why did it change? | The message says 'Simplify (drop Decimal)'. | The reason. Maybe someone thought floats were simpler. Ask the team. |
| Who is affected? | Which code paths use the changed function. | Which customers and invoices. That needs data and the finance systems contact. |
Look at the dates too. The culprit is dated 2025-06-02. The commit that marked the test as flaky is dated 2025-07-14. For six weeks the test failed in the open, and then it was silenced.
01d9417 2025-09-01 Describe price bands in README
b940624 2025-08-04 Document price_order arguments
045ffbc 2025-07-14 Mark test_contract_price_rounding as flaky
bc8bdcb 2025-06-23 Add test for the volume discount boundary
4ad78b0 2025-06-02 Simplify contract pricing (drop Decimal)
72d4982 2025-05-12 Add README note on running tests
703e52c 2025-04-22 Reject negative quantities
1ab5c20 2025-04-07 Use contract pricing for the Contract band
e7be3cf 2025-03-17 Add contract pricing with Decimal rounding
cb26c80 2025-03-03 Add price_order for Standard and Volume bandsThat is a process lesson, not a person to blame. A failing test was labelled flaky without anyone checking. The write-up in the last lesson will record both the technical cause and this gap.
Using an AI assistant with history
An assistant is good at reading a diff and explaining in plain words what changed. It can also help you write the judge command. It cannot run git on your machine, so it cannot know which commit is bad.
- Paste the diff of the commit you found. Ask what behaviour could change and for which inputs.
- Ask it to suggest experiments that confirm the answer. Run them.
- Ask for a bisect judge command, then check its exit codes yourself.
- Remove names, tokens and customer data from commit messages and diffs before you paste them. See Sharing Code with an AI Assistant Safely.
Conclusion and your next step
You can now find the commit that introduced a regression: search history for the suspect code, pick a judge, and let git bisect run halve the range. For the napkin bug, the commit titled 'Simplify contract pricing (drop Decimal)' is the one. You also saw that a test labelled flaky was really telling the truth.
Your next step: bring an assistant into the investigation, and test what it says. Continue with Debugging with an AI Assistant. Then try the exercise on a regression in your own repository.
To go deeper, read Mapping Entry Points and Data Flow for how to follow code across a project, and Recording Decisions in Short Form for how to keep the reasons behind a change.
Reusable prompt
Culprit-commit reader
Asks an assistant to explain what a suspect commit changed in behaviour and to propose experiments, without treating it as the final cause.
Below is a commit that my bisection flagged as the first bad commit. Explain, in plain words, what it changes in behaviour. Do not assume it is the root cause.
Symptom: for contract account TC7-0042, NAP-100 (list price 1.25, 10% discount) gets a unit price of 1.12. Finance policy says round half up to the penny, which gives 1.13.
Commit message: "Simplify contract pricing (drop Decimal)"
Diff:
- from decimal import Decimal, ROUND_HALF_UP
- price = Decimal(str(list_price))
- unit = price * (Decimal(100) - discount) / Decimal(100)
- return unit.quantize(PENNY, rounding=ROUND_HALF_UP)
+ price = float(list_price)
+ unit = price * (1 - discount_percent / 100)
+ return round(unit, 2)
Please:
1. Describe what the old and the new code do differently, step by step.
2. List which inputs would give different results, with two examples I can check by hand.
3. Suggest two small experiments that would confirm or refute your explanation.
4. Say what you cannot know from this diff alone.Show the reusable template
Replace each {{placeholder}} with your own details.
Below is a commit that my bisection flagged as the first bad commit. Explain, in plain words, what it changes in behaviour. Do not assume it is the root cause.
Symptom: {{symptom_with_expected_and_actual}}
Commit message: "{{commit_message}}"
Diff:
{{diff}}
Please:
1. Describe what the old and the new code do differently, step by step.
2. List which inputs would give different results, with two examples I can check by hand.
3. Suggest two small experiments that would confirm or refute your explanation.
4. Say what you cannot know from this diff alone.Expected output and limitations
A step-by-step comparison of old and new behaviour, a few example inputs you can check by hand, two experiments and an honest list of what the diff cannot show. The assistant should talk about this diff, not about floats in general.
Old code: does the sum in Decimal and rounds half up to the penny.
New code: does the sum in float and uses Python's built-in round().
Inputs that may differ:
- 1.25 with 10% off: exact value 1.125. Old gives 1.13. New may give 1.12 (round sends an exact half to the even digit).
- 1.15 with 10% off: exact value 1.035. The float may sit just below it.
Experiments: run both versions on these inputs; print the float with Decimal(value).
Cannot know from the diff: why the change was made, or which customers are affected.Limitations to keep in mind
- The sample is illustrative. The assistant has not run either version.
- It may state how floats behave with confidence. Check each claim with a script.
- It cannot tell you whether this is the only change that matters. Bisection and tests do that.
- A diff pasted with real names, tokens or customer data may break your organisation's rules.
Common errors and how to fix them
Choosing a good commit that does not contain the test
Why it happens: The judge fails with a 'not found' error on old commits, and bisect counts every such failure as 'bad'.
Fix: Pick a good commit where the test exists and passes. Run the judge by hand on both ends first.
Bisecting with a flaky or slow judge
Why it happens: A command that passes and fails at random sends bisect in the wrong direction.
Fix: Make the judge deterministic. Run it three times on each end. Use git bisect skip for commits that do not build.
Forgetting to leave bisect mode
Why it happens: You read the culprit and carried on working in the middle of a bisection.
Fix: Run git bisect reset, then check git status before you edit any file.
Validation checklist
Tick each item once you have confirmed it.
Hands-on exercise
Bisect a regression in your repository
Take a regression you can reproduce in a repository you are allowed to use, write or reuse a test as the judge, and find the first bad commit with git bisect run.
You will need
- A repository with history, a bad commit and a commit you believe was good.
- A test or script that exits with 0 when good and non-zero when bad.
- Your reproduction card and hypothesis log.
Acceptance criteria
Your work is done when every one of these is true.
Hints (3)
- If you have no seeded bug, make a copy of a small repository and change a rounding rule in the middle of its history.
- If bisect lands on a commit that cannot be built, use
git bisect skip. - Use
git log -Sfirst. It can save the whole bisection.
Show a model answer
A good answer shows the judge run on both ends, the complete bisect output, and a short reading of the culprit's diff in plain words. It names what the history cannot say, such as why the change was made or which customers it touched. A weak answer pastes only the final hash, never checked the judge, and calls the author at fault.
Knowledge check
Choose an answer, then check it. Nothing is recorded — this is for you.
Show the answer and explanation
Answer: A. Bisect halves the range between a good and a bad commit and uses the command's exit code as the answer each time. It does not need names or file lists. Those are things you read afterwards.
Show the answer and explanation
Answer: B. A command that fails for any reason counts as 'bad' unless it exits with 125. Bisect cannot tell a missing test from a failing test. So run the judge by hand on the good commit first.
Show the answer and explanation
Answer: B. History shows where behaviour changed, not intent. Blaming a person is wrong and unhelpful. You still need the hypothesis, the diff reading and a fix with a test to understand the root cause.
Frequently asked questions
What is git bisect in simple words?
It is a git feature that finds the commit that broke something. You tell it one good version and one bad version. It tests the version in the middle, then halves the range again, until only one commit is left.
Do I need a test to use git bisect?
You need a judge, which is any command that returns 0 when things are fine and another number when they are not. A test is the best judge, but a small script works too. You can also mark each version by hand as good or bad as git offers it to you.
What does git log -S do?
It lists the commits that added or removed a piece of text, such as round(. It is a quick way to find when a word, a function name or a setting first appeared. It is called a pickaxe search.
How many steps does bisect need?
It halves the range each time, so the number of steps grows very slowly. Eight commits need about three tests. A thousand commits need about ten. This is why it is worth setting up a judge.
- Written by
- PromptFable Editorial
- Last updated 19 Nov 2026
- Review
- Reviewed by Waseem Nadir, Technical Reviewer
- Last reviewed 27 Nov 2026
References
Primary sources for the technical details in this lesson. Vendor platforms change — check the official page if anything here looks different from what you see.
- git-bisect Documentation(opens in a new tab)
Git project · Official documentation · Covers bisect run, exit codes and skipping commits.
- git-log Documentation(opens in a new tab)
Git project · Official documentation · Describes the -S option used for the pickaxe search.
- git-blame Documentation(opens in a new tab)
Git project · Official documentation
- pytest documentation(opens in a new tab)
pytest · Official documentation · For writing a test that makes a reliable judge.
PromptFable is independent and is not affiliated with, endorsed by or sponsored by Anthropic, Google, OpenAI or any other vendor. Claude, Gemini, Google AI Studio and other product names are trademarks of their respective owners.