Tracking Commit History for Specific Lines in Git

Nov 22, 2025 · Programming · 11 views · 7.8

Keywords: Git | commit history | line tracking

Abstract: This article details how to use Git's -L option with git log to retrieve the complete commit history for specific lines in a file. Through step-by-step examples and in-depth analysis, it helps developers efficiently track code changes, complementing git blame's limitations and exploring relevant use cases.

Introduction

In software development, it is often necessary to trace the history of changes to specific lines of code. While the git blame command shows the last commit that modified a line, it does not provide the full commit log for that line. This article explains in detail how to use Git's -L option with the git log command to obtain the commit history for specific lines, including additions, modifications, and deletions.

Using the git log -L Option

Starting from Git version 1.8.4, the git log command includes the -L option to trace the evolution of a specified line range in a file. The basic syntax is git log -L start,end:file, where start and end define the line numbers, and file specifies the target file. For example, to trace the history of line 10 in a file named example.py, use the command git log -L 10,10:example.py. This command outputs all commits that touched the line, along with diff information showing the specific changes.

Step-by-Step Example Demonstration

Assume we have a Python file demo.py with the following code:

def hello():
    print("Hello, World!")
    return True

First, use the git blame command to view the last commit that modified line 2:

git blame -L 2,2 demo.py

The output might show something like: abc123 def hello() (Author 2023-01-01 10:00:00 +0000 2) print("Hello, World!"). Then, use git log -L to get the full history of that line:

git log --pretty=short -u -L 2,2:demo.py

The output will include multiple commits, for example:

commit def456
Author: Jane Doe <jane@example.com>

    Update greeting message

diff --git a/demo.py b/demo.py
--- a/demo.py
+++ b/demo.py
@@ -1,3 +1,3 @@
 def hello():
-    print("Hi, World!")
+    print("Hello, World!")
     return True

commit abc123
Author: John Smith <john@example.com>

    Add hello function

diff --git a/demo.py b/demo.py
--- /dev/null
+++ b/demo.py
@@ -0,0 +1,3 @@
+def hello():
+    print("Hi, World!")
+    return True

This example demonstrates the complete evolution of line 2 from its initial addition as print("Hi, World!") to its modification as print("Hello, World!").

Finding the First Commit That Added a Line

Beyond tracing the history of a specific line, developers often need to find the commit that first added a line. Using the git log -L command, this can be achieved by identifying the earliest commit in the log output. For instance, the first commit in the output typically represents the addition of the line. Additionally, for GitHub repositories, methods from reference materials can be applied, such as using the web interface to search for the line and view its history, but this relies on Git's underlying commands.

In-Depth Analysis and Use Cases

The git log -L option works by analyzing Git's diff data to identify all commits that affect the specified line range. Compared to git blame, it provides a more comprehensive view, making it suitable for code reviews, debugging, and refactoring. For example, when fixing bugs, it helps quickly locate the commit that introduced the issue; in code optimization, it tracks the evolution of specific algorithms. Note that this command may be affected by file renames or moves, so it is advisable to combine it with other Git commands like git log --follow to handle path changes.

Conclusion

By leveraging Git's -L option, developers can efficiently retrieve the commit history for specific code lines, addressing the limitations of git blame. The examples and analysis provided in this article highlight its powerful capabilities, encouraging its application in real-world projects to enhance code maintenance efficiency. Combined with version control best practices, this tool significantly simplifies tracking in complex codebases.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.