Find commits that added or removed a string with git log -S
To find out "when was this function removed?" or "which commit added this number?", pass the string to git log -S and you can search the change history by content. The commits it shows are the ones that changed how many times the string appears in the files, that is, the commits that added it and the commits that removed it. You find them even if the commit message says nothing about it. A commit that rewrites a line without changing the count does not show up, so if you want to find those changes too, use -G.
I tested on Ubuntu 24.04 with Git 2.43.0. I set the author name and email to sample values (Neko <neko@example.com>), and I set the commit dates with environment variables. I did not test on Windows or macOS, or search from GitHub or other web interfaces.
- 1. The history before searching
- 2. Use -S to find the commits that added and removed a string
- 3. Commits that only rewrite a line are not shown by -S
- 4. Narrowing by file
- 5. Show the diffs of other files in the same commit (–pickaxe-all)
- 6. Searching without distinguishing case
- 7. Caveats when using regular expressions with –pickaxe-regex
The history before searching
I used a repository with 8 commits across three files: README.md, shop.js, and docs.md, which holds the explanation. The shipping fee function calcShipping had its condition changed, an argument added, the function order swapped, and then its name changed to shippingFee. In the commit that renamed it, I also added a heading to README.md. The last commit rewrote the name in the explanation as well.
$ git log --oneline
eb7d784 説明書きを直す
c0f7849 関数の名前を変更
df6a601 関数の並びを入れ替え
c967c0a 引数を追加
f6d3d24 条件を変更
00fc617 説明書きを追加
8fdb39b 合計の計算を追加
ed7276a 最初のコミット
The current shop.js no longer has calcShipping. Searching with git grep finds nothing.
$ cat shop.js
function shippingFee(price, area) {
return price >= 5000 ? 0 : 600;
}
function calcTotal(price) {
return price + shippingFee(price);
}
$ git grep -n calcShipping
Use -S to find the commits that added and removed a string
When you put a string after -S, only the commits where the number of occurrences of that string changed are shown. The official reference for -S says it finds changes that alter the number of occurrences of the string, meaning additions and removals.
$ git log --oneline -S calcShipping
eb7d784 説明書きを直す
c0f7849 関数の名前を変更
00fc617 説明書きを追加
8fdb39b 合計の計算を追加
8fdb39b and 00fc617 added calcShipping, and c0f7849 and eb7d784 removed it. None of the commit messages contain the word calcShipping.
You can also search for values. Searching for 3000, the amount for free shipping, shows both the commit that wrote 3000 and the commit that changed it to 5000. Adding -p also shows the diff for each one.
$ git log -p -S "3000" --format="%h %s"
f6d3d24 条件を変更
diff --git a/shop.js b/shop.js
index 2a44a2e..c9a1a4f 100644
--- a/shop.js
+++ b/shop.js
@@ -3,5 +3,5 @@ function calcTotal(price) {
}
function calcShipping(price) {
- return price >= 3000 ? 0 : 500;
+ return price >= 5000 ? 0 : 600;
}
8fdb39b 合計の計算を追加
diff --git a/shop.js b/shop.js
new file mode 100644
index 0000000..2a44a2e
--- /dev/null
+++ b/shop.js
@@ -0,0 +1,7 @@
+function calcTotal(price) {
+ return price + calcShipping(price);
+}
+
+function calcShipping(price) {
+ return price >= 3000 ? 0 : 500;
+}
--format="%h %s" shows only the short hash and the subject line.
Searching by words in the message: –grep is a different search
--grep searches commit messages, while -S searches the contents of files. In this history, no message contains the word for "shipping" (送料), so --grep=送料 finds nothing. -S 送料 finds the commit that wrote that word in docs.md.
$ git log --oneline --grep=送料
$ git log --oneline -S 送料
00fc617 説明書きを追加
I covered how to search by message and date in the article "Searching past commits by words, files, and dates with git log" (Japanese).
Commits that only rewrite a line are not shown by -S
The results of -S calcShipping above leave out c967c0a (add argument) and df6a601 (reorder functions). Both changed lines that contain calcShipping.
$ git show --format="%h %s" c967c0a
c967c0a 引数を追加
diff --git a/shop.js b/shop.js
index c9a1a4f..643d114 100644
--- a/shop.js
+++ b/shop.js
@@ -2,6 +2,6 @@ function calcTotal(price) {
return price + calcShipping(price);
}
-function calcShipping(price) {
+function calcShipping(price, area) {
return price >= 5000 ? 0 : 600;
}
In c967c0a, one line containing calcShipping is removed and one is added. The number of calcShipping occurrences in the file is two both before and after the change. df6a601 only moved functions up or down, so the count does not change either. The official gitdiffcore reference also states that -S does not detect moves within a file.
-G also shows commits that changed a line containing the string
-G shows commits where an added or removed line in the diff matches the regular expression you pass. With -G calcShipping, the two commits that -S missed also appear.
$ git log --oneline -G calcShipping
eb7d784 説明書きを直す
c0f7849 関数の名前を変更
df6a601 関数の並びを入れ替え
c967c0a 引数を追加
00fc617 説明書きを追加
8fdb39b 合計の計算を追加
Because -G takes a regular expression, write ( as \(. Searching for calcShipping(price, area), the function line after the argument was added, shows c967c0a, which wrote this form, and c0f7849, which removed it. Without the \, nothing was shown, because in a regular expression ( has a special meaning.
$ git log --oneline -G "calcShipping\(price, area\)"
c0f7849 関数の名前を変更
c967c0a 引数を追加
$ git log --oneline -G "calcShipping(price, area)"
Use -S if you only want to know when the string was added or removed. Use -G if you want to see every change to a line containing the string. -G also picks up moves that leave the content unchanged, such as reordering, so if you get too many results, switch back to -S to narrow them down.
Narrowing by file
If you put a path after --, only the commits that changed the number of occurrences in that file are shown.
$ git log --oneline -S calcShipping -- shop.js
c0f7849 関数の名前を変更
8fdb39b 合計の計算を追加
$ git log --oneline -S calcShipping -- docs.md
eb7d784 説明書きを直す
00fc617 説明書きを追加
Viewing the content just before a removed function disappeared
With -1, only the last commit that changed the count of calcShipping in shop.js is shown. In this history, that is c0f7849, which renamed the function and removed calcShipping. Running git show on shop.js from the first parent of that commit (c0f7849^) displays the function as it was just before it was removed.
$ git log --oneline -S calcShipping -1 -- shop.js
c0f7849 関数の名前を変更
$ git show c0f7849^:shop.js
function calcShipping(price, area) {
return price >= 5000 ? 0 : 600;
}
function calcTotal(price) {
return price + calcShipping(price);
}
I also cover how to display an old file using the commit:path form in the article "Viewing a file's contents at an old commit with git show" (Japanese).
Show the diffs of other files in the same commit (–pickaxe-all)
When you use -S together with -p, only the files where the number of occurrences of the string changed appear in the diff. c0f7849 also changed README.md, but it is not shown. The final -1 c0f7849 means to show just one commit, starting from c0f7849 and going back.
$ git log -p -S calcShipping --format="%h %s" -1 c0f7849
c0f7849 関数の名前を変更
diff --git a/shop.js b/shop.js
index 0bcaba3..ff861a6 100644
--- a/shop.js
+++ b/shop.js
@@ -1,7 +1,7 @@
-function calcShipping(price, area) {
+function shippingFee(price, area) {
return price >= 5000 ? 0 : 600;
}
function calcTotal(price) {
- return price + calcShipping(price);
+ return price + shippingFee(price);
}
Adding --pickaxe-all shows all the files changed in that commit.
$ git log -p -S calcShipping --pickaxe-all --format="%h %s" -1 c0f7849
c0f7849 関数の名前を変更
diff --git a/README.md b/README.md
index e8f0184..c4c958e 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,5 @@
# Cat's Paw Shop
+
+## Calculation
+
+shop.js にあります。
diff --git a/shop.js b/shop.js
index 0bcaba3..ff861a6 100644
--- a/shop.js
+++ b/shop.js
@@ -1,7 +1,7 @@
-function calcShipping(price, area) {
+function shippingFee(price, area) {
return price >= 5000 ? 0 : 600;
}
function calcTotal(price) {
- return price + calcShipping(price);
+ return price + shippingFee(price);
}
Searching without distinguishing case
-S is case-sensitive. Searching for calcshipping in all lowercase finds nothing. Adding -i lets you search without distinguishing case.
$ git log --oneline -S calcshipping
$ git log --oneline -i -S calcshipping
eb7d784 説明書きを直す
c0f7849 関数の名前を変更
00fc617 説明書きを追加
8fdb39b 合計の計算を追加
Caveats when using regular expressions with –pickaxe-regex
The string you pass to -S is searched as a literal string. Adding --pickaxe-regex treats it as an extended regular expression (official reference for –pickaxe-regex). However, what is counted is the number of matches of the regular expression, so you may get fewer results than you expect.
When I searched for the old and new names together with calcShipping|shippingFee, c0f7849, which renamed the function, and eb7d784, which rewrote the name in the explanation, did not appear. This is because renaming does not change the total number of matches for "one name or the other" (the count for the pattern as a whole stays the same).
$ git log --oneline -S "calcShipping|shippingFee" --pickaxe-regex
00fc617 説明書きを追加
8fdb39b 合計の計算を追加
Similarly, searching for price >= [0-9]+ does not show f6d3d24, which changed 3000 to 5000. Before and after the change, the pattern matches in only one place.
$ git log --oneline -S "price >= [0-9]+" --pickaxe-regex
8fdb39b 合計の計算を追加
If you want to search with a regular expression and also find commits that rewrote a matching line, use -G. The official reference for -G gives examples of combining -S with --pickaxe-regex and how that differs from -G.



