Sub topic 3
Focusing blame on a line range or function with -L
Running git blame on a whole file prints one annotated row per line. In a 2,000-line file, you only care about a handful of those rows. The -L option limits the output to the lines you ask for. It also makes blame faster, because Git only reports on that range.
This chapter assumes you can read basic blame output (hash, author, date, line number, content) and know how to run Git commands in a terminal. For the regular-expression forms you also need basic regex.
Why narrow the output
Suppose a bug points at the parse_config function in src/app.py. Full blame mixes that function with imports, helpers, and unrelated code. Each row also names a different commit, which makes it hard to see the history of the one function you are debugging.
With -L, the output contains only the rows you chose. Line numbers in the output still refer to the whole file, so you can match them against your editor.
Selecting lines by number
The basic form is -L start,end. Both numbers are inclusive.
git blame -L 40,60 src/app.pyOutput rows look like this:
a1b2c3d4 (Dana Lee 2024-03-05 10:12:01 +0000 40) def parse_config(path):
e5f6a7b8 (Sam Ortiz 2024-06-18 16:40:22 +0000 41) with open(path) as fh:Your hashes, names, and dates will differ. A hash starting with ^ marks a boundary commit, which is the oldest commit Git looked at.
Other number forms are shortcuts:
| Form | Meaning |
|---|---|
| `-L 40,60` | Lines 40 through 60 |
| `-L 40,+20` | 20 lines starting at line 40 |
| `-L 40,-5` | 5 lines ending at line 40 |
| `-L 40,` | Line 40 to the end of the file |
| `-L ,60` | Start of the file through line 60 |
The +N and -N forms are handy when you know one line, such as the line in a stack trace, and want some context around it.
Selecting lines by pattern
Line numbers change as a file is edited. A regular expression between slashes finds the start of the range for you.
git blame -L '/def parse_config/,+20' src/app.pyGit finds the first line matching def parse_config and shows that line plus the next 20. You can also use a regex for the end of the range. In that case the end pattern is searched for after the start line.
git blame -L '/def parse_config/,/^def /' src/app.pyThis range runs from def parse_config up to the next line starting with def . The last row of the range is that next def line, because the end of the range is inclusive.
Quote the pattern. Without quotes, your shell may interpret /, ^, or * before Git sees them.
Selecting a function by name
For languages Git understands, you can name the function directly with a leading colon:
git blame -L :parse_config src/app.pyGit works out where the function starts and ends. It uses a built-in pattern, or a language-specific one if the file type is configured through .gitattributes. Note that with git blame the file is a separate argument. This differs from git log -L, where the file is part of the option (:parse_config:src/app.py), which a later chapter covers.
The default pattern treats a line starting with a letter, underscore, or $ as the start of a function. Indented methods inside a Python class will not be recognized as boundaries by that default. If :name selects the wrong span, fall back to a regex range or explicit line numbers.
Using more than one range
You can repeat -L to see several separate regions in one run:
git blame -L 1,10 -L '/def parse_config/,+20' src/app.pyGit prints the ranges in file order, so this is a quick way to compare an import block with the function that uses it.
Choosing the right form
Common mistakes
- Using stale line numbers. Line numbers from an old stack trace may not match your current checkout. Check the revision or use a pattern.
- Forgetting the file name.
-Lselects lines, but you still pass the file path as the last argument. - Pattern matches too early. A regex such as
/parse_config/can match a call site before the definition. Usedef parse_configto be specific. - Unquoted patterns. The shell may expand or alter them.
Exercise
You want to see who last changed the 15 lines that end at line 120 of src/app.py, and also the function load_settings. Write one command that shows both.
Show answer
git blame -L 106,120 -L :load_settings src/app.py (the form -L 120,-15 is also valid for the first range). Git prints both ranges in file order.