Inspection
Line history (git log -L)
3 min read
Line history shows the commits that touched a range of a file, and only those. For each commit, the diff shows only what changed in that range. Git tracks the range through edits (lines inserted above push the range down) and through the file's renames.
It's the direct answer to questions like "when did this function get this parameter?" or "who
touched this if in the last few months?", without reading the file's whole history.
- In blame: click a line number and, with Shift+click, the last one's number. In the strip that appears, click Line history.
- In a blame block's menu: History of this range (lines X–Y) or History of this line (N), for the whole block.
- In the diff: pick lines by clicking their numbers and use Line history ("Commits that
touched these lines (git log -L)") in the strip of picked lines.
- In the diff of a commit, between two commits or of a comparison, lines are picked only for this, because there's no staging.
- In the diff of your changes (unstaged or staged), the same button sits next to Stage lines and Discard lines.
- Open the file's blame, or the diff of a commit that touched it.
- Click the number of the range's first line and Shift+click the number of the last one. The strip shows "lines 40–58".
- Click Line history. While git searches, "Looking for the commits that touched these lines…" appears.
- The Range history screen opens in the central area. The header shows the file, the lines, the starting commit ("starting at <sha>") and how many commits were found.
- Choose a commit in the list to see, on the right, the message and the diff of the range only. If the file had another name in that commit, the row shows "In that commit: <path>".
- Use the reload button to search again and the X or Esc to close.
If the picked lines include any added (+) or context line, the numbers from the new side of the diff apply. If you picked only removed (−) lines, the ones from the old side apply.
| Where you picked the lines | New side | Removed lines only |
|---|---|---|
| Blame of a version | The blame's version | — |
| Blame of the working directory | The disk | — |
| A commit's diff | The commit | The commit's parent |
| Diff between two commits | The newer commit | The older commit |
| "commit × working directory" diff | The disk | The commit |
| Staged diff | Staging | HEAD |
| Unstaged diff | The disk | Staging |
git log -L only works with commits. That's why lines picked on disk or in staging are mapped to
the equivalent HEAD numbers. Lines that don't exist in HEAD yet become the range around them, and
the screen tells you.
Read only:
git log -L<start>,<end>:<file> -n <count> <starting-commit>
To convert the numbers from the disk or staging to those of HEAD:
git diff -U0 --no-ext-diff [--cached] HEAD -- <file>
The list shows at most the 200 most recent commits.
There's nothing to undo: the screen only reads the repository.
- "Some of the chosen lines aren't in any commit yet: this is the history of the range around them in HEAD.": you picked lines that only exist on disk or in staging. The result is approximate.
- "No commit touched these lines.": the range has no history from the starting commit.
- "There are more commits before these (the list shows the 200 most recent).": the range has a long history. Pick a smaller range or start from an older commit.
- "New file: it has no history yet": the button is disabled on an untracked file.
- "Pick lines that have a number": none of the picked lines has a line number. Pick code lines from the diff.
- "The file "<file>" doesn't exist in <sha>: it has no history yet.": the file didn't exist yet in the starting commit.
- "Line N doesn't exist in this version: the file has M lines.": the range goes past the end of the file in that version.
- "The repository has no commits yet: there's no history to show.": make the first commit.
Tip
To follow a single line back to where it was born, even when it came from another file or arrived through a merge, use Line origin.
Line history has no preference of its own and no company policy key.
- Blame
- Line origin
- File history
- Diff
- Git documentation: git log -L (opens another site)