Sub topic 14
Following a file across renames
This chapter assumes you can run git log with a path filter and read --name-status output. It also assumes you know that a commit points to its parent.
Why history seems to stop
Git does not record renames. Each commit stores a snapshot of files, and a rename is just one path disappearing and another appearing. When you ask for the history of a path, Git walks back through commits that touched that exact path.
git log --oneline -- src/config.cIf src/config.c was once called src/settings.c, this command stops at the commit that created the new name. Everything before the rename is missing, and the output gives no warning.
Using --follow
--follow makes git log keep tracking one file after a rename. When it reaches a commit where the file appears to have been renamed, it switches to the old path and continues.
git log --follow --oneline -- src/config.cThe output now includes commits made under the old name. Add -p or --stat to see the changes in each commit. The rename commit itself shows the file at both paths.
How Git detects a rename
Git compares the files removed and added in a commit. If an added file is similar enough to a removed one, Git reports a rename. The default threshold is 50% similarity. You can see this with --name-status:
git show --name-status -M HEADA rename appears as a status letter followed by the similarity score:
R095 src/settings.c src/config.cR095 means a rename with 95% similarity. R100 means the content is identical. You can set the threshold yourself with -M80%.
Copies and moved code
-C extends detection to copies. Git then looks for new files that resemble files modified in the same commit.
git log --follow -C --oneline -- src/config.c
git diff -M -C --name-status HEAD~1 HEADUse -C when a file was split or duplicated. Use -M for plain renames. Copy detection is slower on large commits, so enable it only when you need it.
Limits and common errors
--followworks on one file only. Giving it a directory does not follow renames inside it.- Put
--before the path, so Git does not read the path as a branch name. --followis a heuristic. If a file was heavily rewritten in the same commit as the rename, similarity may fall below the threshold and tracking stops there.- A file that was deleted and later re-created is treated as unrelated history.
Diffing across a rename
git diff between two commits also detects renames by default in current Git versions. If the output shows a deleted file and a new file instead, ask for detection explicitly:
git diff -M --stat main featureA detected rename appears as old => new in the summary, so you review only the edits and not the whole file.
Exercise
A teammate renamed utils.py to helpers.py last month. Running git log --oneline -- helpers.py shows only commits from the last month. Which command shows the full history, and why did the first command stop early?
Show answer
Run git log --follow --oneline -- helpers.py. Git stores snapshots and not renames, so a plain path filter only matches commits that touched the exact path helpers.py. --follow detects the rename by similarity and continues with the old path utils.py.