Use git log --follow -p to see every commit that touched a file, including renames, and git blame to see who last changed each line and why.
The short answer
#Pass the file path to git log. Add --follow so the history continues past renames, and -p to see the actual change in each commit. To see who last changed each line, use git blame.
_10git log --follow -p -- src/utils/format.ts_10git blame src/utils/format.ts
Why it happens
#Git does not store a history per file. It stores snapshots of the whole project, one per commit. When you ask for a file's history, Git walks the commits and keeps only the ones where that path changed.
That is why renames trip people up. If format.js became format.ts three months ago, a plain git log -- format.ts stops at the rename, because before that commit no file had that path. --follow tells Git to detect the rename and keep following the old name. It only works with a single file, not a directory.
The -- separator tells Git that what follows is a path, not a branch name. You can usually leave it out, but it avoids confusion when a file and a branch share a name.
How to fix it
#List the commits that touched a file
Start with a compact list, then dig into the commits that look interesting.
_10# One line per commit_10git log --oneline --follow -- src/utils/format.ts_10_10# Add the diff for each commit_10git log --follow -p -- src/utils/format.ts_10_10# Just the size of each change_10git log --follow --stat -- src/utils/format.ts
To see the file exactly as it was in an older commit, use git show with the commit and the path.
_10git show a1b2c3d:src/utils/format.ts
Find who changed each line with git blame
git blame prints every line with the commit, author and date of its last change. It answers "why is this line here?" quickly, because you can take the commit hash and run git show on it to read the full message.
_10# Only lines 40 to 60_10git blame -L 40,60 src/utils/format.ts_10_10# Ignore whitespace changes and follow lines moved or copied from other files_10git blame -w -C src/utils/format.ts
If a big formatting commit hides the real history, list its hash in a file and tell blame to skip it.
_10echo "a1b2c3d4e5f6..." >> .git-blame-ignore-revs_10git config blame.ignoreRevsFile .git-blame-ignore-revs
GitHub and GitLab also read a .git-blame-ignore-revs file in the repository root for their blame views.
Follow a single function over time
git log -L tracks a range of lines, or a function by name, through history. It shows each commit that changed that part of the file along with the diff.
_10# By line range_10git log -L 40,60:src/utils/format.ts_10_10# By function name_10git log -L :formatPrice:src/utils/format.ts
The function form relies on Git recognising function boundaries for that language, which works well for most C-like languages, including JavaScript and TypeScript.
Search for when some code appeared or vanished
If you know a piece of text but not where it lived, the "pickaxe" options search the diffs themselves.
_10# Commits that changed the number of times this string appears_10git log -S "legacyFormat" --oneline_10_10# Commits whose diff matches a regular expression_10git log -G "format(Price|Date)" --oneline
Common pitfalls
#- Forgetting
--follow****. Without it, history stops at the last rename and older commits look missing. - Using
--followon a folder. It only works for one file. For a directory, usegit log -- src/utils/without it. - Blaming the formatter. A mass reformat makes
git blamepoint at one commit for every line. Use-wor an ignore-revs file. - Reading blame as fault. Blame shows the last change, not who wrote the original logic. Check earlier commits with
git log -Lbefore drawing conclusions. - Looking at the wrong branch.
git logshows history reachable from your current branch. Add a branch name, or--all, to look elsewhere.
Related reading
#Was this resource helpful?