Code archaeology with git: find out why code is the way it is
Use git blame, log -S, log -L, shortlog and pull request history to discover why code was written, who knows it and what changed. Commands with examples.
Code tells you what happens. Git history often tells you why. When you meet a strange condition, a magic number or a workaround with no comment, the commit that introduced it, and the pull request around that commit, usually explain it. These commands are the core toolkit.
git blame, used well
git blame -w -C -C -C path/to/file.ts-wignores whitespace changes, so reformatting commits don't hide the real author.-C -C -Cdetects lines moved or copied from other files, so blame follows code across refactors.
Most editors show blame inline, and GitHub's blame view lets you click "view blame prior to this change" to step back through history.
If your team keeps a .git-blame-ignore-revs file listing bulk formatting commits, use it with --ignore-revs-file.
Find when something appeared: git log -S
git log -S "MAX_RETRIES" --onelineThe "pickaxe" finds commits that changed the number of occurrences of a string, so it shows when a constant, function or config key was added or removed. Use -G with a regex to find commits where matching lines changed at all.
Follow one function's history: git log -L
git log -L :calculateTotals:src/pricing.tsThis shows every commit that changed the calculateTotals function, with diffs limited to that function. It's one of git's most useful and least known features. You can also give a line range: -L 40,80:src/pricing.ts.
Follow a file through renames
git log --follow -p -- src/pricing.ts--follow keeps tracking the file across renames, and -p shows each diff.
Find who knows an area
git shortlog -sn --since="1 year ago" -- src/payments/This counts commits per author for a folder over the past year. The top names are the people to ask, and if there's only one, that's a bus factor worth knowing about.
Find the hot spots
git log --since="6 months ago" --format=format: --name-only | sort | uniq -c | sort -rn | head -20Files that change most often are where bugs and complexity concentrate. Combined with file size, this is a quick way to find the code that matters most.
Read the pull request, not just the commit
Commit messages are often short. The pull request holds the discussion, the linked issue, the review comments and sometimes screenshots. On GitHub, search the commit hash, or open the commit and follow the link to its PR.
A worked example
You find this:
if (order.total > 0 && order.total < 0.5) order.total = 0.5;git blameon that line gives a commit from two years ago.- The commit message says "Fix Stripe minimum".
- The PR links an issue: payments under $0.50 were failing because of the payment provider's minimum charge.
Now you know the line is deliberate, why it exists and what would break if you removed it.
Combine history with current behaviour
History explains why; current code shows what. Ask RepoGrasp what a function does today and where it's called (with citations), then use git log -L to see how it got that way. That combination answers most "can I change this?" questions.