Skip to content
Laviyx Learn

Try

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.

Advanced6 min readUpdated
On this page

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:

Terminal
git blame cart.txt
Output
^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=light

Use 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:

Terminal
git blame -L 2,3 cart.txt
Output
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

GitHub has the same feature: open a file and click Blame.

Search through history#

git grep searches files in the working tree:

Terminal
git grep -n "tax"
Output
cart.txt:2:tax=-5

git log -S is called the pickaxe. It finds commits that added or removed a piece of text:

Terminal
git log --oneline -S "tax=-5"
Output
f6ce8bd Tidy cart settings

git 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:

Terminal
git bisect start
git bisect bad HEAD
git bisect good HEAD~6
Output
status: 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 option

Git has checked out a commit halfway through. Test it. Here, the test is to look at the tax line:

Terminal
grep tax cart.txt
Output
tax=5

The tax is still positive, so this commit is good. Tell Git, and it picks the next commit to test:

Terminal
git bisect good
grep tax cart.txt
Output
Bisecting: 0 revisions left to test after this (roughly 1 step)
[cac113dfc086630343d6ccbe824e1bf6e42b723a] Add coupon field
tax=-5

The tax is negative, so this commit is bad:

Terminal
git bisect bad
grep tax cart.txt
Output
Bisecting: 0 revisions left to test after this (roughly 0 steps)
[f6ce8bd7b65060aee98b0adc13e21b6e7ee0303f] Tidy cart settings
tax=-5

Bad again. Git now has its answer:

Terminal
git bisect bad
Output
f6ce8bd7b65060aee98b0adc13e21b6e7ee0303f is the first bad commit
commit f6ce8bd7b65060aee98b0adc13e21b6e7ee0303f
Author: Ravi Kumar <ravi@example.com>
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:

Terminal
git bisect reset
Output
Previous 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.

Terminal
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>&1
Output
f6ce8bd7b65060aee98b0adc13e21b6e7ee0303f is the first bad commit

Common 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
Terminal
git log -n 1 --format='%an: %s' -L 2,2:cart.txt -s
Output
Ravi Kumar: Tidy cart settings

Key takeaway#

Next: Git Hooks.

Finished this lesson?