More Git answers

by Danny
Published 2 min read

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.


_10
git log --follow -p -- src/utils/format.ts
_10
git 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
_10
git log --oneline --follow -- src/utils/format.ts
_10
_10
# Add the diff for each commit
_10
git log --follow -p -- src/utils/format.ts
_10
_10
# Just the size of each change
_10
git 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.


_10
git 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
_10
git blame -L 40,60 src/utils/format.ts
_10
_10
# Ignore whitespace changes and follow lines moved or copied from other files
_10
git 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.


_10
echo "a1b2c3d4e5f6..." >> .git-blame-ignore-revs
_10
git 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
_10
git log -L 40,60:src/utils/format.ts
_10
_10
# By function name
_10
git 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
_10
git log -S "legacyFormat" --oneline
_10
_10
# Commits whose diff matches a regular expression
_10
git log -G "format(Price|Date)" --oneline

Common pitfalls

#
  • Forgetting --follow****. Without it, history stops at the last rename and older commits look missing.
  • Using --follow on a folder. It only works for one file. For a directory, use git log -- src/utils/ without it.
  • Blaming the formatter. A mass reformat makes git blame point at one commit for every line. Use -w or 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 -L before drawing conclusions.
  • Looking at the wrong branch. git log shows history reachable from your current branch. Add a branch name, or --all, to look elsewhere.
#

Was this resource helpful?