Sub topic 9
Following a function or file through history with git log -L and --follow
This chapter assumes you can read git log output, run git blame, and know what git log -S does. You should also know basic commit, branch and diff terms. Blame shows the last commit to touch each line. Sometimes you need the whole sequence of changes to one function or one file. Two options cover that: git log -L and git log --follow.
What git log -L does
git log -L traces a line range or a function through history. For each commit that changed that range, it prints the commit header and a diff limited to that range. Other changes in the same commit are left out.
The range syntax is -L start,end:file, or -L :funcname:file to name a function.
git log -L :parse_config:src/app.pyGit finds parse_config in the file by using a function-name pattern for the file type. It then walks back through history and shows each commit that changed that function, with the diff for that function only.
You can also give a line range, the same way you do with blame:
git log -L 40,60:src/app.pyThe range is tracked as the file changes. If lines are inserted above the range in an earlier commit, Git adjusts the line numbers as it walks back.
Reading the output
Each entry has the usual commit hash, author, date and message. A diff follows, and it contains only hunks that fall inside the traced range.
The newest commit comes first. Reading down the output is reading backward in time. The last entry is usually the commit that created the function. Read the entries from the bottom up if you want the story in order.
Choosing between -L, -S and blame
The three tools answer different questions.
| Question | Tool |
|---|---|
| Who last changed this line? | `git blame` |
| When did this string first appear or vanish? | `git log -S` |
| How did this function evolve over time? | `git log -L` |
Use -L when you want every change to a function, not only the latest one. Use -S when the code may no longer exist in the file.
Function detection and its limits
For :funcname: to work, Git must recognize where a function starts and ends. It uses built-in patterns for common languages. It picks the pattern from the file's diff attribute. If Git guesses wrong, it may trace too many lines or too few.
You can set the pattern in .gitattributes:
*.py diff=python
*.c diff=cppIf the name does not match a function, Git reports an error. In that case, fall back to a line range or a regular expression:
git log -L '/def parse_config/,/^def /:src/app.py'This starts the range at the first line matching def parse_config and ends it at the next line matching ^def .
Following a file through renames with --follow
A file's history stops at a rename unless you ask Git to continue. git log -- path only lists commits where the file had that exact name.
--follow tells Git to detect renames and keep going under the old name:
git log --follow --oneline -- src/config.pyIf src/config.py used to be src/settings.py, the output includes commits from before the rename.
--follow has limits:
- It works on a single file only. You must give exactly one path.
- Rename detection is based on similarity. If a commit renames a file and also rewrites most of it, Git may treat it as a delete and a new file.
- It is heuristic and can occasionally follow the wrong file.
git log -L follows renames and copies on its own in recent Git versions, so you do not need to add --follow to it. Check your version's behavior if the trace stops at a rename.
Combining the options
You can add the usual git log filters. For example, to see only recent changes to a function with a one-line format, skip the diff and use -s:
git log -s --since=2025-01-01 --oneline -L :parse_config:src/app.pyGit 2.x versions differ on whether -s suppresses the -L diff. Run it once and check the result on your version.
Common errors
- "no match" or "Invalid -L": the function name or file path does not match. Check the spelling and that the file exists at
HEAD. - Output covers too much code: Git did not recognize the function end. Set a
diff=attribute or use explicit line ranges. - History stops early: the file was renamed or the function was moved. Run
--followfor the file, or search for the string withgit log -S.
Exercise
A function load_settings in src/config.py was changed several times. The file was renamed from src/settings.py last year. Which two commands would you use to list every commit that touched the function, and the commits that touched the file under both names?
Show answer
Use git log -L :load_settings:src/config.py to see each change to the function with function-only diffs. Use git log --follow --oneline -- src/config.py to list every commit to the file, including those made when it was named src/settings.py.