Git and GitHub course menu
git blame and git bisect: Find Who Changed What and Which Commit Broke It
Use git blame to see who last changed each line, git log -S to find when code appeared, and git bisect to find the exact commit that introduced a bug with a binary search.
Something broke, and you need to know when and why. Git's history can answer that for you.
git blame#
git blame shows who last changed each line, and in which commit:
git blame cart.txt^6a0e5b5 (Asha Rao 2026-11-05 09:00:00 +0530 1) price=100
f6ce8bd7 (Ravi Kumar 2026-11-05 09:20:00 +0530 2) tax=-5
b5b8f9c1 (Ravi Kumar 2026-11-05 09:05:00 +0530 3) shipping=40
016a5d50 (Asha Rao 2026-11-05 09:10:00 +0530 4) note=ok
cba4fb0b (Asha Rao 2026-11-05 09:15:00 +0530 5) gift=no
cac113df (Asha Rao 2026-11-05 09:25:00 +0530 6) coupon=none
d3f891d5 (Asha Rao 2026-11-05 09:30:00 +0530 7) theme=lightUse it to find the commit that explains a line, then read that commit with git show. Despite its name, it is about finding context, not fault.
For part of a file, give a line range:
git blame -L 2,3 cart.txtf6ce8bd7 (Ravi Kumar 2026-11-05 09:20:00 +0530 2) tax=-5
b5b8f9c1 (Ravi Kumar 2026-11-05 09:05:00 +0530 3) shipping=40GitHub has the same feature: open a file and click Blame.
Search through history#
git grep searches files in the working tree:
git grep -n "tax"cart.txt:2:tax=-5git log -S is called the pickaxe. It finds commits that added or removed a piece of text:
git log --oneline -S "tax=-5"f6ce8bd Tidy cart settingsgit bisect#
git blame helps when you know which line is wrong. If you only know that something worked last month and is broken now, use git bisect. It runs a binary search through history. Each step halves the number of commits left to check, so even 1,000 commits take only about 10 steps.
In this repository, tax should be positive, but it is now negative. We know the latest commit is bad, and the first commit was good. Start the search, and mark them both:
git bisect start
git bisect bad HEAD
git bisect good HEAD~6status: waiting for both good and bad commits
status: waiting for good commit(s), bad commit known
Bisecting: 2 revisions left to test after this (roughly 2 steps)
[cba4fb0bac059e01ffcc369d10454f1384ba436b] Add gift optionGit has checked out a commit halfway through. Test it. Here, the test is to look at the tax line:
grep tax cart.txttax=5The tax is still positive, so this commit is good. Tell Git, and it picks the next commit to test:
git bisect good
grep tax cart.txtBisecting: 0 revisions left to test after this (roughly 1 step)
[cac113dfc086630343d6ccbe824e1bf6e42b723a] Add coupon field
tax=-5The tax is negative, so this commit is bad:
git bisect bad
grep tax cart.txtBisecting: 0 revisions left to test after this (roughly 0 steps)
[f6ce8bd7b65060aee98b0adc13e21b6e7ee0303f] Tidy cart settings
tax=-5Bad again. Git now has its answer:
git bisect badf6ce8bd7b65060aee98b0adc13e21b6e7ee0303f is the first bad commit
commit f6ce8bd7b65060aee98b0adc13e21b6e7ee0303f
Author: Ravi Kumar <[email protected]>
Date: Thu Nov 5 09:20:00 2026 +0530
Tidy cart settings
cart.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)Git names the first bad commit, and shows that it was the one called Tidy cart settings. When you have finished, always return to where you started:
git bisect resetPrevious HEAD position was f6ce8bd Tidy cart settings
Switched to branch 'main'Automate it#
If you have a test, script or command that exits with 0 for good and a non-zero code for bad, Git can do the whole search for you. In real projects this is often a test command such as npm test.
git bisect start HEAD HEAD~6 > /dev/null
git bisect run sh -c "! grep -q 'tax=-' cart.txt" | grep 'is the first bad commit'
git bisect reset > /dev/null 2>&1f6ce8bd7b65060aee98b0adc13e21b6e7ee0303f is the first bad commitCommon mistakes#
Practice#
Practice exercise
Print the author and subject of the commit that last changed line 2 of cart.txt.
Show hint
git blame -s -L 2,2 cart.txt prints the commit ID. Pass its first column to git show, or use git log -L 2,2:cart.txt.
Show solution
git log -n 1 --format='%an: %s' -L 2,2:cart.txt -sRavi Kumar: Tidy cart settingsKey takeaway#
Next: Git Hooks.
Finished this lesson?