How to Trace the History of One Function with git log -L
Running git log -L :funcname:file shows only the commits that changed one function in a file, along with the diff of just that function. You can find the change that caused a bug, or find out when and why some logic took its current form, without scanning the whole file's history by eye. I ran the git commands in a test environment and included the output as it was.
The test environment was Ubuntu 24.04 with Git 2.43.0. I set the author name and email to example values (Neko <neko@example.com>). I saved the output to a file, so it has no colors and no pager was used. I did not test on Windows or macOS, with a setting in .gitattributes that changes how functions are found, with merge commits in the history, or with a file that was moved and had its contents changed in the same commit.
- 1. The repository before the investigation
- 2. Following the history by function name (-L :funcname:file)
- 3. Changes outside the function are not shown
- 4. Showing only the list of commits (–no-patch)
- 5. Specifying a range by line numbers (-L start,end:file)
- 6. Specifying a range with regular expressions (-L /start/,/end/:file)
- 7. Specifying the commit to start from
- 8. Invalid specifications and errors
- 9. If you only moved the file, you can follow the history from before the move
The repository before the investigation
I prepared a repository called neko-shop. It has src/cart.js, which contains calcShipping to calculate the shipping fee and calcTotal to compute the total, and it has six commits.
$ git log --oneline
bdce423 離島の送料を分ける
ab36a1d 送料を600円に上げる
a4b470d README を追加
11125a3 3000円以上で送料無料にする
8482c82 合計に消費税を足す
8301386 最初のコミット
The current src/cart.js is as follows. The line numbers are added with cat -n.
$ cat -n src/cart.js
1 const SHIPPING_FEE = 600;
2 const ISLAND_FEE = 1200;
3
4 function calcShipping(sum, island = false) {
5 if (sum >= 3000) return 0;
6 if (island) return ISLAND_FEE;
7 return SHIPPING_FEE;
8 }
9
10 function calcTotal(items) {
11 const sum = items.reduce((a, i) => a + i.price, 0);
12 const tax = Math.floor(sum * 0.1);
13 return sum + tax + calcShipping(sum);
14 }
15
16 module.exports = { calcTotal, calcShipping };
Suppose you want the history of calcShipping. With git log -- src/cart.js, which specifies the file, all five commits except the README commit appear. Changes unrelated to calcShipping are mixed in, such as the commit that added the consumption tax.
$ git log --oneline -- src/cart.js
bdce423 離島の送料を分ける
ab36a1d 送料を600円に上げる
11125a3 3000円以上で送料無料にする
8482c82 合計に消費税を足す
8301386 最初のコミット
Following the history by function name (-L :funcname:file)
After -L, write the function name and the file separated by :, as in :calcShipping:src/cart.js.
$ git log -L :calcShipping:src/cart.js
commit bdce4231cfb9034f57eea4a5233d8c6b3060e358
Author: Neko <neko@example.com>
Date: Thu Nov 12 12:30:00 2026 +0900
離島の送料を分ける
diff --git a/src/cart.js b/src/cart.js
--- a/src/cart.js
+++ b/src/cart.js
@@ -3,5 +4,6 @@
-function calcShipping(sum) {
+function calcShipping(sum, island = false) {
if (sum >= 3000) return 0;
+ if (island) return ISLAND_FEE;
return SHIPPING_FEE;
}
commit 11125a338c6f61939afb27f0830b717dd184b89b
Author: Neko <neko@example.com>
Date: Thu Nov 12 11:00:00 2026 +0900
3000円以上で送料無料にする
diff --git a/src/cart.js b/src/cart.js
--- a/src/cart.js
+++ b/src/cart.js
@@ -3,4 +3,5 @@
function calcShipping(sum) {
+ if (sum >= 3000) return 0;
return SHIPPING_FEE;
}
commit 830138648ae53d114629440e86fc5d0ffb11186c
Author: Neko <neko@example.com>
Date: Thu Nov 12 10:00:00 2026 +0900
最初のコミット
diff --git a/src/cart.js b/src/cart.js
--- /dev/null
+++ b/src/cart.js
@@ -0,0 +3,4 @@
+function calcShipping(sum) {
+ return SHIPPING_FEE;
+}
+
Three commits appeared: the one that created calcShipping, the one that added the free-shipping condition, and the one that added the island argument. The consumption tax commit did not appear. The diff also covers only the calcShipping part, not the whole file. @@ -3,5 +4,6 @@ means that before the change, it covers 5 lines starting at line 3, and after the change, 6 lines starting at line 4. Because one line for the island constant was added, the function moved down by one line.
The explanation of -L in the official git log reference says that <funcname> is a regular expression that matches the function name, and that the range runs from the line of the matched function up to the line just before the next function. This is why the blank line after } is included in the diff above. The rule for finding function lines is the same as the one used for the hunk headers in git diff. According to the official gitattributes reference section on defining a custom hunk header, if you configure nothing, lines that begin with a letter, _, or $ are treated as function lines.
Changes outside the function are not shown
ab36a1d 送料を600円に上げる changed the shipping fee that calcShipping returns. However, what it changed was the constant SHIPPING_FEE on line 1 of the file, not something inside the function. So it did not appear in the result of -L :calcShipping.
-L only looks at the lines in the range you specify. Changes to constants a function uses, or to other functions it calls, do not show up. So when you look for the cause of a change in a function's behavior, also specify the lines for those.
You can write -L more than once. When I added lines 1–2 of the constants with -L 1,2:src/cart.js, the commit that changed the shipping fee to 600 yen also appeared. To keep the output short, I used --oneline to put each commit's information on one line.
$ git log --oneline -L :calcShipping:src/cart.js -L 1,2:src/cart.js
bdce423 離島の送料を分ける
diff --git a/src/cart.js b/src/cart.js
--- a/src/cart.js
+++ b/src/cart.js
@@ -1,1 +1,2 @@
const SHIPPING_FEE = 600;
+const ISLAND_FEE = 1200;
@@ -3,5 +4,6 @@
-function calcShipping(sum) {
+function calcShipping(sum, island = false) {
if (sum >= 3000) return 0;
+ if (island) return ISLAND_FEE;
return SHIPPING_FEE;
}
ab36a1d 送料を600円に上げる
diff --git a/src/cart.js b/src/cart.js
--- a/src/cart.js
+++ b/src/cart.js
@@ -1,1 +1,1 @@
-const SHIPPING_FEE = 500;
+const SHIPPING_FEE = 600;
11125a3 3000円以上で送料無料にする
diff --git a/src/cart.js b/src/cart.js
--- a/src/cart.js
+++ b/src/cart.js
@@ -3,4 +3,5 @@
function calcShipping(sum) {
+ if (sum >= 3000) return 0;
return SHIPPING_FEE;
}
8301386 最初のコミット
diff --git a/src/cart.js b/src/cart.js
--- /dev/null
+++ b/src/cart.js
@@ -0,0 +1,1 @@
+const SHIPPING_FEE = 500;
However, in the test environment (Git 2.43.0), the first commit 8301386 showed only the diff for the range of lines 1–2, and the part that created calcShipping did not appear. I also tried other ranges such as -L 1,1 and -L 10,14, and changed the order in which the -L options were written. In every case, the first commit showed only the diff for the line 1 range. If you want to see the first form of each range, running the -L options one at a time is reliable.
Showing only the list of commits (–no-patch)
When you add -L, the diff is shown even if you do not specify anything else. If you want to know only which commits are involved, add --no-patch. -s gave the same result.
$ git log --oneline --no-patch -L :calcShipping:src/cart.js
bdce423 離島の送料を分ける
11125a3 3000円以上で送料無料にする
8301386 最初のコミット
The official reference says that -L implies --patch (showing the diff), and that you can turn it off with --no-patch. It also says that other diff output formats, such as --name-only, --name-status, --numstat, and --shortstat, are not yet supported.
Specifying a range by line numbers (-L start,end:file)
You can also write the range with line numbers instead of a function name. -L 1,2:src/cart.js covers lines 1 through 2, and -L 4,+5:src/cart.js covers 5 lines starting at line 4 (lines 4–8). Line numbers are written using the line numbers of the current file.
$ git log --oneline -L 4,+5:src/cart.js
bdce423 離島の送料を分ける
diff --git a/src/cart.js b/src/cart.js
--- a/src/cart.js
+++ b/src/cart.js
@@ -3,4 +4,5 @@
-function calcShipping(sum) {
+function calcShipping(sum, island = false) {
if (sum >= 3000) return 0;
+ if (island) return ISLAND_FEE;
return SHIPPING_FEE;
}
11125a3 3000円以上で送料無料にする
diff --git a/src/cart.js b/src/cart.js
--- a/src/cart.js
+++ b/src/cart.js
@@ -3,3 +3,4 @@
function calcShipping(sum) {
+ if (sum >= 3000) return 0;
return SHIPPING_FEE;
}
8301386 最初のコミット
diff --git a/src/cart.js b/src/cart.js
--- /dev/null
+++ b/src/cart.js
@@ -0,0 +3,3 @@
+function calcShipping(sum) {
+ return SHIPPING_FEE;
+}
The same three commits appeared as when specifying by function name. Because the range ends on the } line, the blank line after it is not included in the diff. Git follows the line shifts and shows the range that started at line 3 in the earlier commits.
Specifying a range with regular expressions (-L /start/,/end/:file)
Line numbers shift when you edit the file. If you write the start and end as regular expressions, you can set the range without looking up line numbers. I specified the range from the line where calcTotal is defined to the next } at the start of a line.
$ git log --oneline -L "/^function calcTotal/,/^}/:src/cart.js"
8482c82 合計に消費税を足す
diff --git a/src/cart.js b/src/cart.js
--- a/src/cart.js
+++ b/src/cart.js
@@ -7,4 +7,5 @@
function calcTotal(items) {
const sum = items.reduce((a, i) => a + i.price, 0);
- return sum + calcShipping(sum);
+ const tax = Math.floor(sum * 0.1);
+ return sum + tax + calcShipping(sum);
}
8301386 最初のコミット
diff --git a/src/cart.js b/src/cart.js
--- /dev/null
+++ b/src/cart.js
@@ -0,0 +7,4 @@
+function calcTotal(items) {
+ const sum = items.reduce((a, i) => a + i.price, 0);
+ return sum + calcShipping(sum);
+}
For calcTotal, two commits appeared: the one that created it and the one that added the consumption tax. According to the official reference's explanation of how to write a line range, the end regular expression is searched for downward from the start line. So /^}/ matches the first } at the start of a line after calcTotal. The official reference also includes an example, git log -L '/int main/',/^}/:main.c, which shows changes to main().
Specifying the commit to start from
If you write one commit after -L, the history is traced back from that commit. If you write a range such as 11125a3.., only commits after 11125a3 are shown.
$ git log --oneline --no-patch -L :calcShipping:src/cart.js 11125a3
11125a3 3000円以上で送料無料にする
8301386 最初のコミット
$ git log --oneline --no-patch -L :calcShipping:src/cart.js 11125a3..
bdce423 離島の送料を分ける
According to the official reference, the starting commit (positive revision) can be zero or one. The specified function or lines must exist in the file as it was at that starting commit.
Invalid specifications and errors
If the function name is not found, the command stops with an error.
$ git log -L :calcPoint:src/cart.js
fatal: -L parameter 'calcPoint' starting at line 1: no match
You cannot omit the file name. If the form is not :funcname:file, you get the following error.
$ git log -L :calcShipping
fatal: -L argument not 'start,end:file' or ':funcname:file': :calcShipping
The -- file form (pathspec) that is often used with git log cannot be used together with -L. Write the file inside -L.
$ git log -L :calcShipping:src/cart.js -- src/cart.js
fatal: -L<range>:<file> cannot be used with pathspec
If you only moved the file, you can follow the history from before the move
I moved src/cart.js to src/lib/cart.js with git mv and committed it. Looking at the file's history with -- src/lib/cart.js shows only the commit that moved it.
$ git log --oneline -- src/lib/cart.js
783bca7 cart.js を lib に移す
When I specified the file in its new location with -L, commits from the time of the old src/cart.js also appeared.
$ git log --oneline -L :calcShipping:src/lib/cart.js
bdce423 離島の送料を分ける
diff --git a/src/cart.js b/src/cart.js
--- a/src/cart.js
+++ b/src/cart.js
@@ -3,5 +4,6 @@
-function calcShipping(sum) {
+function calcShipping(sum, island = false) {
if (sum >= 3000) return 0;
+ if (island) return ISLAND_FEE;
return SHIPPING_FEE;
}
11125a3 3000円以上で送料無料にする
diff --git a/src/cart.js b/src/cart.js
--- a/src/cart.js
+++ b/src/cart.js
@@ -3,4 +3,5 @@
function calcShipping(sum) {
+ if (sum >= 3000) return 0;
return SHIPPING_FEE;
}
8301386 最初のコミット
diff --git a/src/cart.js b/src/cart.js
--- /dev/null
+++ b/src/cart.js
@@ -0,0 +3,4 @@
+function calcShipping(sum) {
+ return SHIPPING_FEE;
+}
+
The file name in the diff is the name of src/cart.js at each commit. The commit 783bca7, which only moved the file, does not appear because it did not change the contents of the function. In this example, where the file was moved without changing its contents, I could trace the history back to before the move without any option such as --follow.
Finally, I checked the state after the investigation. git log -L only displays history, so the working tree has no changes, and the commits have not increased from the move.
$ git status --short
$ git log --oneline
783bca7 cart.js を lib に移す
bdce423 離島の送料を分ける
ab36a1d 送料を600円に上げる
a4b470d README を追加
11125a3 3000円以上で送料無料にする
8482c82 合計に消費税を足す
8301386 最初のコミット



