A wrong line and no clue
The price list shows a VAT rate of 21 percent and nobody remembers why. The file has ten commits of history, the repository thirty-one: reading them one by one costs an afternoon. Git has three commands built for this question, and they answer three different questions. git log -S<string> lists the commits that made that string appear or disappear, because they change the number of its occurrences in a file. git log -G<regex> lists the commits where an added or removed line matches the regular expression, even when the number of occurrences does not change. git blame -L tells you, line by line, which commit touched it last. The first takes you to the origin of a text, the second to every change that concerns it, the third to the state of today.
To choose well you need to know what each one hides from you: -S does not see a move, -G also sees reformatting, and blame points at whoever fixed the spaces and not at whoever wrote the logic. I checked everything on a test repository, with real outputs. The level is intermediate: it assumes you know the commit and how to read a git log, and it is for whoever works on code they did not write.
Every block was run on 2026-11-19 with Git 2.53.0 (git --version) in a repository created in a temporary folder outside the project, without ever publishing anything. I ran them twice, with my system language and with LC_ALL=C, and the outputs are identical: the commands in this article print no translated messages. In the blocks, the line that starts with $ is the command and the lines below it are its output.
The test repository
To search history you need a history. A Bash script creates 31 commits with fixed dates, one every two days starting on 3 March 2026, attributed to three made-up authors with example.invalid addresses.
I do not reproduce it in full, because the part that matters is the content of the files and the function that records each commit, which you find below, together with a .gitattributes file containing the line *.py diff=python, which I discuss later. The identifiers in your repository will differ from mine. The commit messages, the file names and the constants are in Italian.
N=0
c() { # c "autore" "messaggio"
N=$((N+1))
d=$(date -u -d "2026-03-01 +$((N*2)) days 10:00" +%Y-%m-%dT%H:%M:%S)
case "$1" in
anna) nm="Anna Rossi"; em="anna@example.invalid";;
luca) nm="Luca Verdi"; em="luca@example.invalid";;
*) nm="Marta Neri"; em="marta@example.invalid";;
esac
git add -A
GIT_AUTHOR_NAME="$nm" GIT_AUTHOR_EMAIL="$em" GIT_COMMITTER_NAME="$nm" GIT_COMMITTER_EMAIL="$em" \
GIT_AUTHOR_DATE="$d +0000" GIT_COMMITTER_DATE="$d +0000" git commit -q -m "$2"
}
echo "*.py diff=python" > .gitattributes
The project is a small price-calculation module. Almost all the commits are noise, such as changelog entries and notes in the README, and only ten touch prezzi.py. It is the proportion you find in a real repository, where the line you are after is one among many.
The block shows the number of commits, who signed them, the file as it is today and its history in one line per commit.
$ git log --oneline | wc -l
31
$ git shortlog -sn HEAD
19 Marta Neri
7 Anna Rossi
5 Luca Verdi
$ cat prezzi.py
IVA = 22
def prezzo_finito(netto):
return netto * (100 + IVA) / 100
def calcola_sconto(totale, percentuale, tetto_max):
tetto = min(percentuale, tetto_max)
return totale * tetto / 100
def arrotonda(valore):
return round(valore, 2)
$ git log --format='%h %ad %an %s' --date=short -- prezzi.py
cc40e3f 2026-04-30 Luca Verdi Il tetto dello sconto arriva dal listino
a6f497a 2026-04-22 Anna Rossi Formattazione uniforme del modulo
e20f9a0 2026-04-20 Luca Verdi Spazi nelle parentesi, per errore
96a9dfa 2026-04-12 Anna Rossi Arrotondamento a due decimali
23695dc 2026-04-10 Anna Rossi Costanti in testa al modulo
2dcd578 2026-04-02 Luca Verdi Il tetto dello sconto scende al 15 per cento
5d36f19 2026-03-31 Anna Rossi Ripristino l'aliquota IVA
0090397 2026-03-25 Luca Verdi Aggiorno l'aliquota
14599b8 2026-03-15 Luca Verdi Sconto sul totale con tetto al 20 per cento
3292682 2026-03-05 Anna Rossi Calcolo del prezzo con IVA
Four facts are hidden in this history. The VAT rate went from 22 to 21 and then back to 22. The discount cap started at 20, dropped to 15 and vanished when it became a parameter. A constant was moved to the top of the module. And two commits changed only the formatting, one by mistake and one to fix it. These are the cases where the three commands behave differently.
git log -S: counting the occurrences of a string
The git log documentation describes -S as the search for differences that change the number of occurrences of the string in a file, that is, additions and deletions. Without --pickaxe-regex the string is literal, and you pass it in quotes if it contains spaces.
In the first block I search for IVA = 21 and shape the output with --format and --date=short to get date and author. Two commits appear: 0090397 made it appear, 5d36f19 made it disappear.
The second command adds -p and limits the search to the file with -- prezzi.py. It is the combination you will use most often: the path filter limits the search to that file, and the patch shows you the effect without opening the commit.
Here it confirms the story in two lines of diff, from 22 to 21 and back. Whoever wrote IVA = 21 is Luca, on 25 March, with a message that says nothing about the reason: the clue you need for the next question lies in the message and the date, not in the code.
$ git log --format='%h %ad %an %s' --date=short -S'IVA = 21'
5d36f19 2026-03-31 Anna Rossi Ripristino l'aliquota IVA
0090397 2026-03-25 Luca Verdi Aggiorno l'aliquota
$ git log -S'IVA = 21' -p --format='%h %s' -- prezzi.py
5d36f19 Ripristino l'aliquota IVA
diff --git a/prezzi.py b/prezzi.py
index 474d632..25e0f5d 100644
--- a/prezzi.py
+++ b/prezzi.py
@@ -1,4 +1,4 @@
-IVA = 21
+IVA = 22
SCONTO_MAX = 20
0090397 Aggiorno l'aliquota
diff --git a/prezzi.py b/prezzi.py
index 25e0f5d..474d632 100644
--- a/prezzi.py
+++ b/prezzi.py
@@ -1,4 +1,4 @@
-IVA = 22
+IVA = 21
SCONTO_MAX = 20
The number of occurrences is also the limit of -S. I search for SCONTO_MAX without the value: I get only the commit that introduced it and the one that removed it, and the step from 20 to 15 is missing, because in that commit the constant was there before and was there after.
If I include the value instead, SCONTO_MAX = 20 returns the commit that introduced it and the one that changed it, and SCONTO_MAX = 15 the one that changed it and the one that removed it. The string you choose decides what you see, and the trick is to search for the value you suspect, not for the name.
$ git log --oneline -S'SCONTO_MAX'
cc40e3f Il tetto dello sconto arriva dal listino
14599b8 Sconto sul totale con tetto al 20 per cento
$ git log --oneline -S'SCONTO_MAX = 20'
2dcd578 Il tetto dello sconto scende al 15 per cento
14599b8 Sconto sul totale con tetto al 20 per cento
$ git log --oneline -S'SCONTO_MAX = 15'
cc40e3f Il tetto dello sconto arriva dal listino
2dcd578 Il tetto dello sconto scende al 15 per cento
git log -G: every change to a line that matches
With -G the criterion changes: Git looks at the text of the patch and selects the commits with added or removed lines that match the regular expression. It does not count occurrences, so a line that moves, or a change that leaves the count unchanged, still gets through.
In the block I compare the two commands on the IVA constant: -S'IVA = 22' returns three commits, -G'IVA = ' returns four. The fourth is 23695dc, the one that moved the constants.
The next block shows why. In the patch of 23695dc the line IVA = 22 appears once removed and once added, in a different position: for -S the count in the file is the same before and after, for -G there is a matching line that changes. It depends on how Git builds the patch: in this case it represented the swap by moving the VAT line, and that is why it is the one that shows up.
$ git log --oneline -S'IVA = 22'
5d36f19 Ripristino l'aliquota IVA
0090397 Aggiorno l'aliquota
3292682 Calcolo del prezzo con IVA
$ git log --oneline -G'IVA = '
23695dc Costanti in testa al modulo
5d36f19 Ripristino l'aliquota IVA
0090397 Aggiorno l'aliquota
3292682 Calcolo del prezzo con IVA
$ git show --format='%h %s' 23695dc -- prezzi.py
23695dc Costanti in testa al modulo
diff --git a/prezzi.py b/prezzi.py
index 22c4276..3c57257 100644
--- a/prezzi.py
+++ b/prezzi.py
@@ -1,5 +1,5 @@
-IVA = 22
SCONTO_MAX = 15
+IVA = 22
def prezzo_finito(netto):
The documentation contains exactly this case: a line that changes without changing the occurrences is shown by -G and not by -S with --pickaxe-regex. The price of -G is noise, and you see it by searching for SCONTO_MAX: of the five commits returned, two are the reformattings I will discuss shortly, which touched lines with that name without changing anything of substance.
$ git log --oneline -G'SCONTO_MAX'
cc40e3f Il tetto dello sconto arriva dal listino
a6f497a Formattazione uniforme del modulo
e20f9a0 Spazi nelle parentesi, per errore
2dcd578 Il tetto dello sconto scende al 15 per cento
14599b8 Sconto sul totale con tetto al 20 per cento
I sum up the choice with the question you are asking yourself:
- "Since when does this string exist?" and "When was it removed?":
-S, which returns few commits and does not see moves within the same file. - "Who has ever touched a line that looks like this?":
-G, which also finds moves and reformatting, and returns more commits to read. - "Any number after
IVA =": a regular expression, with-Gor with-Sand--pickaxe-regex, and the two do not give the same result, as the next block shows.
The difference shows with the case that comes to mind first: searching for every value of IVA with a digit class. With --pickaxe-regex, which according to the documentation treats the -S string as a POSIX extended regular expression, I get a single commit, the first: in the others the line changes from one number to another, and the number of lines matching the expression stays one.
With -G and the same expression I get four. If you are looking for "every change of value", -S with the regex hides exactly the change.
$ git log --oneline --pickaxe-regex -S'IVA = [0-9]+'
3292682 Calcolo del prezzo con IVA
$ git log --oneline -G'IVA = [0-9]+'
23695dc Costanti in testa al modulo
5d36f19 Ripristino l'aliquota IVA
0090397 Aggiorno l'aliquota
3292682 Calcolo del prezzo con IVA
git blame -L: who touched these lines last
git blame annotates each line with the revision that last modified it. On a file of a thousand lines it is useless, so you restrict it with -L. Of the forms the documentation lists, three are enough:
-L 8,10: a range of absolute line numbers, counted from 1;-L '/def arrotonda/,+2': starts at the first line that matches the expression and extends for two lines in all, the first included, as the test showed;-L :calcola_sconto: names a function.
For the third form the documentation says that the range goes from the first matching line up to the next "function line", determined the way git diff works out hunk headers. You will see the consequence in the block: the range does not stop at the last statement of the function, but runs up to the next one.
The output has one row per line of code: the abbreviated commit identifier, the author, the date with the time, the line number and the text. Look at the result for the function. The first and second lines are Luca's, from 30 April, when he added the parameter.
The third, return totale * tetto / 100, is Anna's, from 22 April: she did not write it, she only fixed the spaces. And the two blank lines at the end belong to the range, because the next function starts only after them. The .gitattributes file is not needed: I cancelled the attribute with a *.py !diff line in .git/info/attributes, git check-attr reported unspecified instead of python, and the function's range stayed the same (last block of this section).
$ git blame -L 8,10 prezzi.py
cc40e3fb (Luca Verdi 2026-04-30 10:00:00 +0000 8) def calcola_sconto(totale, percentuale, tetto_max):
cc40e3fb (Luca Verdi 2026-04-30 10:00:00 +0000 9) tetto = min(percentuale, tetto_max)
a6f497a7 (Anna Rossi 2026-04-22 10:00:00 +0000 10) return totale * tetto / 100
$ git blame -L :calcola_sconto prezzi.py
cc40e3fb (Luca Verdi 2026-04-30 10:00:00 +0000 8) def calcola_sconto(totale, percentuale, tetto_max):
cc40e3fb (Luca Verdi 2026-04-30 10:00:00 +0000 9) tetto = min(percentuale, tetto_max)
a6f497a7 (Anna Rossi 2026-04-22 10:00:00 +0000 10) return totale * tetto / 100
96a9dfa0 (Anna Rossi 2026-04-12 10:00:00 +0000 11)
96a9dfa0 (Anna Rossi 2026-04-12 10:00:00 +0000 12)
$ git blame -L '/def arrotonda/,+2' prezzi.py
a6f497a7 (Anna Rossi 2026-04-22 10:00:00 +0000 13) def arrotonda(valore):
a6f497a7 (Anna Rossi 2026-04-22 10:00:00 +0000 14) return round(valore, 2)
$ git check-attr diff -- prezzi.py
prezzi.py: diff: python
$ git check-attr diff -- prezzi.py
prezzi.py: diff: unspecified
$ git blame -L :calcola_sconto prezzi.py
cc40e3fb (Luca Verdi 2026-04-30 10:00:00 +0000 8) def calcola_sconto(totale, percentuale, tetto_max):
cc40e3fb (Luca Verdi 2026-04-30 10:00:00 +0000 9) tetto = min(percentuale, tetto_max)
a6f497a7 (Anna Rossi 2026-04-22 10:00:00 +0000 10) return totale * tetto / 100
96a9dfa0 (Anna Rossi 2026-04-12 10:00:00 +0000 11)
96a9dfa0 (Anna Rossi 2026-04-12 10:00:00 +0000 12)
When blame points at the wrong commit
The line return totale * tetto / 100 was born in 14599b8, on 15 March, together with the function. Blame attributes it to a formatting commit because, by definition, it points at whoever modified it last, and a space added or removed is a modification.
To see beyond that there are two tools, with different effects. The -w option ignores whitespace when comparing the parent's version with the child's. The --ignore-rev option ignores entirely the commits you name: according to the documentation, the lines an ignored commit changed or added are attributed to the previous commit that changed those lines or nearby lines.
Here the block shows a trap. Ignoring only a6f497a, the "right" formatting, the line is attributed to e20f9a0, the "wrong" formatting that came before it, because that one also changed the spaces. To reach 14599b8 you must list both commits.
I do it with --ignore-revs-file, which accepts a file in the same format as fsck.skipList: I wrote the full identifiers in it, obtained with git rev-parse, one per line. With -w instead no list is needed, but it works only when the disturbing commit changes whitespace and nothing else.
$ git blame -L 8,10 -w prezzi.py
cc40e3fb (Luca Verdi 2026-04-30 10:00:00 +0000 8) def calcola_sconto(totale, percentuale, tetto_max):
cc40e3fb (Luca Verdi 2026-04-30 10:00:00 +0000 9) tetto = min(percentuale, tetto_max)
14599b86 (Luca Verdi 2026-03-15 10:00:00 +0000 10) return totale * tetto / 100
$ git blame -L 8,10 --ignore-rev a6f497a prezzi.py
cc40e3fb (Luca Verdi 2026-04-30 10:00:00 +0000 8) def calcola_sconto(totale, percentuale, tetto_max):
cc40e3fb (Luca Verdi 2026-04-30 10:00:00 +0000 9) tetto = min(percentuale, tetto_max)
e20f9a0a (Luca Verdi 2026-04-20 10:00:00 +0000 10) return totale * tetto / 100
$ git rev-parse a6f497a e20f9a0 > .git-blame-ignore-revs
$ git blame -L 8,10 --ignore-revs-file .git-blame-ignore-revs prezzi.py
cc40e3fb (Luca Verdi 2026-04-30 10:00:00 +0000 8) def calcola_sconto(totale, percentuale, tetto_max):
cc40e3fb (Luca Verdi 2026-04-30 10:00:00 +0000 9) tetto = min(percentuale, tetto_max)
14599b86 (Luca Verdi 2026-03-15 10:00:00 +0000 10) return totale * tetto / 100
The four results read like this, from the simplest to the longest to maintain:
- no options: blame points at
a6f497a, the formatting, and says nothing about the logic; -w: points at14599b8, but works only because the disturbing commit changed nothing but spaces;--ignore-revwith a single commit: points ate20f9a0, another formatting commit, because the line had two;--ignore-revs-filewith both: points at14599b8: it ignores the listed commits entirely, whatever they changed, but I tested it only with two formatting commits.
If a repository has recurring mass reformattings, the sensible choice is a file of commits to ignore, committed in the project. The documentation names the variable blame.ignoreRevsFile, which points to a file of that kind and is read before the command-line options. I did not test it: I used the --ignore-revs-file option, and I set no configuration.
There is a second limit, which you overcome by lowering a threshold. The VAT of 22 sits at line 1, and blame attributes it to 23695dc, the one that moved it, and not to 5d36f19, which put the right value back.
The documentation describes -M as the option for lines moved within the file; it is an option with a threshold, the minimum number of alphanumeric characters Git must detect as moved, 20 by default. IVA = 22 has five. With -M without a number the output does not change, with -M5 blame goes back to 5d36f19. The risk of such a low threshold is attributing short, trivial lines by resemblance: I did not measure it. git log -L on the line, instead, still stops at the move, and git log -S takes you to the commit that made that value appear.
$ git blame -L 1,1 prezzi.py
23695dcc (Anna Rossi 2026-04-10 10:00:00 +0000 1) IVA = 22
$ git blame -M -L 1,1 prezzi.py
23695dcc (Anna Rossi 2026-04-10 10:00:00 +0000 1) IVA = 22
$ git blame -M5 -L 1,1 prezzi.py
5d36f19c (Anna Rossi 2026-03-31 10:00:00 +0000 1) IVA = 22
$ git log --oneline --no-patch -L 1,1:prezzi.py
23695dc Costanti in testa al modulo
git log -L: the history of a function
Blame shows the current state. If you want the history of a range, every version it has had, you need git log -L, which the documentation describes as tracing the evolution of a range of lines or of a function in a file.
It has two constraints that I found on the page: it implies --patch, so to get only the list you add --no-patch, and it accepts no path limiters, because the file is already in the argument. In the block the five commits are the ones that touched the range of the function calcola_sconto: the two formatting ones, the one that created it, the one that changed it last, and 96a9dfa, which only added the following function and shows up because of the two blank lines in the range. The second command, with -n 1, shows the most recent patch.
$ git log --oneline --no-patch -L :calcola_sconto:prezzi.py
cc40e3f Il tetto dello sconto arriva dal listino
a6f497a Formattazione uniforme del modulo
e20f9a0 Spazi nelle parentesi, per errore
96a9dfa Arrotondamento a due decimali
14599b8 Sconto sul totale con tetto al 20 per cento
$ git log --oneline -L :calcola_sconto:prezzi.py -n 1
cc40e3f Il tetto dello sconto arriva dal listino
diff --git a/prezzi.py b/prezzi.py
--- a/prezzi.py
+++ b/prezzi.py
@@ -9,5 +8,5 @@
-def calcola_sconto(totale, percentuale):
- tetto = min(percentuale, SCONTO_MAX)
+def calcola_sconto(totale, percentuale, tetto_max):
+ tetto = min(percentuale, tetto_max)
return totale * tetto / 100
Which command for which question
Put together, the three tools cover three different moments of an investigation. A practical rule, which follows from what you saw above:
- you know the string and want the origin:
git log -S'string' -p -- file; - you know the theme and want every change:
git log -G'regex', and you accept a few noise commits; - you see a line today and want who touched it:
git blame -L, with-wor with the list of commits to ignore; - you want the history of a function:
git log -L :function:file.
I did not measure the cost: on 31 commits I timed nothing, and such a time would say nothing about a repository with a hundred thousand commits. From the documentation of the three commands I derive no figure, and I write none.
A suggestion that remains valid is to restrict the search with a path or a range of commits, for example between two tags like those in Git Tag; I did not test this restriction here.
There remains the case where you have no string but a behaviour that broke: then you need a test, and the right command is bisection, described in stash, reflog and bisect.
The trade-off between the two is this: -S works if you know what to look for and does not require running code, bisect works if you know how to recognise the defect and does not require knowing where it is.
What you can do now
You can find the commit that made a string appear or disappear with git log -S, and you know why it does not show you the move of a line or the change of a value if the name stays the same. You can use -G when you need every change to a line, and you know it brings noise. You can limit blame to a function or a range, and you know it says who modified last and not who wrote the logic; to get around reformatting you use -w or a list of commits to ignore. What I did not test stays out: the blame.ignoreRevsFile setting, the behaviour of -C and the timings on a large repository.
Frequently asked questions
What is the difference between git log -S and git log -G?
The first looks for the commits that change the number of occurrences of a string in a file, so it sees additions and deletions. The second looks for the commits with added or removed lines that match a regex, and it also sees moves.
How do I limit git blame to a function?
With -L :function_name, or with a range of lines such as -L 8,10. The range of a function runs up to the next function line, so it may include the blank lines at the end.
How do I ignore a formatting commit in blame?
With --ignore-rev for one commit, or with --ignore-revs-file for a list. If the line went through several formatting commits, all of them must be listed, otherwise blame stops at the first one not ignored.
Sources
- git-log - official Git documentation -
-S,-G,--pickaxe-regex,--pickaxe-alland-Lwith its limitations; read locally withgit help -m logand reachable on the web on 2026-11-19 (HTTP 200, checked with curl) - git-blame - official Git documentation -
-Lfor ranges and functions,-w,-M,--ignore-rev,--ignore-revs-fileandblame.ignoreRevsFile; read locally withgit help -m blameand reachable on the web on 2026-11-19 (HTTP 200, checked with curl) - Examples run with Git 2.53.0 (
git --version) in a test repository of 31 commits in a temporary folder, on 2026-11-19, with the system language and withLC_ALL=C.
Commenti
Caricamento commenti...
Sign in to comment