Lesson 2: The Archaeologist (Git Forensics)
A bug appeared. Nobody knows when it was introduced. The code has hundreds of commits. How do you find the exact commit that broke things? Time for some Git archaeology.
git blame — Who Wrote This Line?
git blame shows who last modified each line of a file, and in which commit:
git blame app.js
a1b2c3d4 (Alice 2025-05-10) const port = 3000;
e5f6g7h8 (Bob 2025-05-12) const host = '0.0.0.0';
i9j0k1l2 (Charlie 2025-05-15) const debug = true; // <-- Who set this?!
Now you know Charlie turned on debug mode on May 15th. Time for a chat! 😄
git bisect — The Binary Search Detective
git bisect uses binary search to find the exact commit that introduced a bug. It's incredibly efficient — even with 1000 commits, it only needs ~10 checks.
# 1. Start the investigation
git bisect start
# 2. Mark the current (broken) commit as bad
git bisect bad
# 3. Mark a known working commit as good
git bisect good abc123
# Git checks out a commit in the middle. Test it, then:
git bisect good # If this commit works
git bisect bad # If this commit is broken
# Git narrows down until it finds THE commit
# 4. When done:
git bisect reset
Searching Through History
Search commit messages:
git log --grep='fix' --oneline # Commits mentioning "fix"
git log --grep='JIRA-123' --oneline # Commits for a specific ticket
Search for code changes:
git log -S 'password' --oneline # Commits that added/removed "password"
git log -S 'api_key' --all --oneline # Search across ALL branches
This is called the pickaxe search — perfect for finding when a function was added or when a secret was accidentally committed.
Search by author:
git log --author='Alice' --oneline # All of Alice's commits
Search by date:
git log --since='2025-05-01' --until='2025-05-15' --oneline
git reflog — The Safety Net
Accidentally deleted a branch? Reset too far? git reflog shows EVERY action you've taken, even those not in git log:
git reflog
a1b2c3d HEAD@{0}: reset: moving to HEAD~3
e4f5g6h HEAD@{1}: commit: Add feature
You can recover lost commits:
git checkout e4f5g6h # Go back to the lost commit
Mission Objective
Become a Git detective:
- Interrogate: Run
git blame readme.txtto see who wrote each line. - Search messages: Run
git log --grep='fix' --onelineto find fix commits. - Search code: Run
git log -S 'password' --onelineto find dangerous changes.
Pro Tip
If someone accidentally committed a password or API key, use git log -S 'secret_key' --all to find it, then use tools like BFG Repo Cleaner to purge it from the entire history.