Tracking Down a Bug: Blame and Bisect
Last updated on 2026-11-02 | Edit this page
Overview
Questions
- Which commit last changed this line, and what was it for?
- When did this test start failing?
- How do I fix a bug I find, without tangling the fix into my own feature?
Objectives
- Use blame in VS Code to see which commit changed a line
- Use
git bisectto find the commit where a test started failing - Fix a bug on a separate branch, merge it into
main, and rebase your feature onto the result
One test is failing
At the end of the last episode we rebased onto main and
one test failed. It isn’t in our code. Run it again:
OUTPUT
> assert np.allclose(result, [0, 0.5, 1])
E assert False
E + where False = <function allclose at 0x769723b210b0>(array([-1. , -0.6, -0.2]), [0, 0.5, 1])
normalise([1, 3, 5]) should give
[0, 0.5, 1], but it gives [-1, -0.6, -0.2].
This test passed at v0.1.0, so something changed in the
colleague’s six commits that affects it. Two questions: which
commit changed normalise, and when did the
test start failing?
Which commit changed this line? Blame
Open peakfinder/normalise.py in VS Code. The suspicious
line is in the range branch. git blame tells
you which commit last changed each line, and its
author, so you know where to look and who to ask about what the change
was for.
In VS Code:
- Open the Command Palette (
Ctrl+Shift+P, orCmd+Shift+Pon a Mac). - Run Git: Toggle Git Blame Editor Decoration. Each line now shows its commit at the end. (Git: Toggle Git Blame Status Bar Item shows the commit for the current line in the status bar instead.)
- Hover over the blame text to see the commit hash, message and author.
On the command line, the same information is:
OUTPUT
e2c75380 (Robin Researcher 2025-02-04 11:00:00 +0000 5) """Scale y. mode is 'range' (0 to 1), 'max' (divide by max) or 'area'."""
b8055cf7 (Sam Scientist 2025-01-06 12:00:00 +0000 6) y = np.asarray(y, dtype=float)
e2c75380 (Robin Researcher 2025-02-04 11:00:00 +0000 7) if mode == "range":
e2c75380 (Robin Researcher 2025-02-04 11:00:00 +0000 8) return (y - y.min()) / y.max() - y.min()
...
The suspicious line 8 comes from e2c75380.
(-L 5,12 means lines 5 to 12.)
Other ways to look at history
- Blame isn’t proof. It shows only the last change to a line. A later commit that merely re-indented the code would hide the real change. For when something broke, use bisect, below.
- The Timeline view (in the Explorer sidebar, for the open file) lists the commits that changed it. The Source Control Graph lists the commits on your branch: select one to see its files, and a file to see the diff.
-
git log -S"text" -- <file>lists the commits that added or removed some text, andgit log -L :<function>:<file>shows the history of one function. - The GitLens and Git History extensions add richer inline blame and file history, if you like them.
Bisect: find when it broke
Blame told us who last touched the line. What we really want is the
first commit where the test fails, and git can find
that for us, quickly. You know a commit where the test passed
(v0.1.0) and one where it fails (main).
git bisect does a binary search between
them: it checks out the commit in the middle, tests it, and halves the
range again.
flowchart LR
accTitle: {Bisect narrows seven commits down to the first bad one}
accDescr: {Seven commits in order from oldest to newest. The oldest, v0.1.0, is known good, shown in green. The newest, Document Peak, is known bad, shown in red. Bisect tests the middle commit first, Remove import, which is bad. It then tests Add mode option, which is bad, and then Fix typo, which is good. So Add mode option is the first bad commit.}
A["v0.1.0<br/>GOOD"] --> B["Fix typo<br/>test 3: good"] --> C["Add mode option<br/>test 2: bad<br/>FIRST BAD"] --> D["Remove import<br/>test 1: bad"] --> E["Bump version"] --> F["Return Peak"] --> G["Document Peak<br/>BAD"]
classDef good fill:#dfd,stroke:#080,color:#000
classDef bad fill:#fdd,stroke:#c00,color:#000
class A,B good
class C,D,G bad
Time runs left to right. With six commits to check it takes three tests. With 1,000 commits, it takes about ten.
Even better, if the test can be run by a command, git can do the
whole search by itself. The command should exit with 0 for “good” and
non-zero for “bad”, which is exactly what pytest does:
(git bisect start <bad> <good>: first the
bad commit, then the good one. Always finish with
git bisect reset, which puts you back on your own
branch.)
OUTPUT
e2c7538061b16250b842ab506cac3c2ae25b20c2 is the first bad commit
commit e2c7538061b16250b842ab506cac3c2ae25b20c2
Author: Robin Researcher <robin.researcher@example.org>
Add mode option to normalise
...
bisect found first bad commit
By hand: what bisect run is doing
Without run, you do the testing yourself. Your
instructor will show this on screen:
BASH
git bisect start main v0.1.0 # git checks out a commit in the middle
pytest -q tests/test_normalise.py # it fails...
git bisect bad # ...so tell git
pytest -q tests/test_normalise.py # git moved to an older commit. It fails too...
git bisect bad
pytest -q tests/test_normalise.py # older still. This one passes
git bisect good # git: "... is the first bad commit"
git bisect reset
At each step you’re on a detached HEAD
(episode 2): not on any branch. That’s fine, as
git bisect reset puts you back.
Why atomic commits matter here
Bisect names a commit. If that commit is small and atomic (“Add mode option to normalise”), you know almost exactly where the problem is. If it were a huge commit that changed 900 lines across ten files, bisect would tell you only that the problem is somewhere in there. This is the payoff of episode 3.
Challenge: find the commit
- Run the three commands above to find the commit where
test_normalise_runs_from_zero_to_onestarts to fail. - Open that commit (
git show <hash>, or select it in the Source Control Graph). What exactly is wrong with therangeline? - Check you’re back on your feature branch:
git branch --show-current.
- The first bad commit is
Add mode option to normalise. - In
rangemode, the code is(y - y.min()) / y.max() - y.min(). The brackets are wrong: it divides byy.max()only, and then subtractsy.min(). It should divide by(y.max() - y.min()). It only gives the right answer when the minimum is zero. The existing test would have caught it, but it looks as though the tests weren’t run, and nothing runs them automatically. (That is what we’ll fix in episode 12.) -
git bisect resetreturns you to your branch.
If git bisect complains about local changes, you have
uncommitted work. Stash it (see the reference page) or commit it, and
try again.
Fix it, but not here
We know what’s wrong, and the fix is one line. The tempting thing is to fix it on the feature branch, as it’s right there. Don’t. The fix has nothing to do with your feature, so your pull request would mix two ideas. And if the fix is tangled up with your feature, nobody can use it without taking your feature too. We’ll need exactly that in episode 13.
So put the fix on its own branch from main:
Fix the line in peakfinder/normalise.py:
Check it, and commit it with a message that explains why:
BASH
pytest
git commit -am "Fix 'range' mode of normalise for spectra that don't start at zero" -m "The division only applied to y.max(), not to (y.max() - y.min())."
On a real team you would open a pull request for this fix, and we’ll
look at doing that well in the next episode. To keep moving, we’ll merge
it straight into main now. Nothing has happened on
main since we branched, so git can
fast-forward it (episode 9):
OUTPUT
Updating 1c13652..05c314d
Fast-forward
peakfinder/normalise.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Finally, your feature branch is based on a main that
still has the bug. Rebase it again, as we did in the last episode:
BASH
git switch feature-prominence # or feature-widths
git rebase main
pytest
git push --force-with-lease
OUTPUT
Successfully rebased and updated refs/heads/feature-prominence.
9 passed
This time there is no conflict, because the fix touched different lines from your feature, and every test passes.
Challenge: fix the bug on its own branch
Do the whole thing yourself:
- Create
fix/normalisefrommain, fix the bug, and commit it with a good message. - Fast-forward
mainto it, push, and delete the branch. - Rebase your feature branch onto the updated
main, and push with--force-with-lease. - Check:
pytestpasses everything, andgit log --oneline -5on your feature branch shows the fix underneath your three commits.
BASH
git switch main
git switch -c fix/normalise
# edit normalise.py: (y - y.min()) / (y.max() - y.min())
pytest
git commit -am "Fix 'range' mode of normalise for spectra that don't start at zero"
git switch main
git merge --ff-only fix/normalise
git push
git branch -d fix/normalise
git switch feature-prominence
git rebase main
pytest
git push --force-with-lease
OUTPUT
ab0c4ef Test min_prominence
b70cf77 Add min_prominence option to find_peaks
6eaff85 Add prominence function
b676f77 Fix 'range' mode of normalise for spectra that don't start at zero
1c13652 Document Peak in the README
(Your hashes will be different.)
Knowledge check
- When might
git blamepoint you at the wrong commit? - Why does
git bisectwork best with small, atomic commits? - Why did we fix the bug on a separate branch, and not on the feature branch?
- When a later commit changed the line without changing what it
does, such as re-indenting, renaming a variable or
reformatting. Blame shows only the last change, not the
one that introduced the bug. (
git blame -wignores whitespace changes, and the VS Code settinggit.blame.ignoreWhitespacedoes the same.) - It names a single commit. If the commit is small and has one purpose, you know where the bug is. A giant commit leaves you searching inside it.
- A fix is a separate idea. Mixing it into the feature would give reviewers two things to understand at once, and anyone who wanted just the fix would have to take the feature too.
- Blame shows which commit last changed each line (in VS Code: Git: Toggle Git Blame Editor Decoration). It shows only the last change, so it isn’t proof of the cause
-
git bisectbinary-searches history for the first commit where a test fails.git bisect run <command>automates it. Always finish withgit bisect reset - Fix bugs you find on a branch of their own, so the fix is a single idea that can be reviewed and reused