Is there an easy way to find out at what commit a certain function stopped being used? There have been too many times I find a function that I thought I was using, but nothing seems to be calling it, which leaves me wondering when and why I did this.
-
2This is a job for [`git bisect`](https://git-scm.com/docs/git-bisect) helped by `grep` or a similar tool. – axiac Jul 26 '16 at 19:16
-
http://stackoverflow.com/questions/3368590/show-diff-between-commits – Jossie Calderon Jul 26 '16 at 19:16
-
@axiac thank you, I will check it out. – Daniel Larsson Jul 26 '16 at 19:21
-
@JossieCalderon I was hoping to find a less manual way – Daniel Larsson Jul 26 '16 at 19:22
-
Do you know what file or folder the function was called in? – Bryce Drew Jul 26 '16 at 20:15
-
@axiac or `git log -S` or `git log -G`, which might be simpler for `git`-non-gurus... – twalberg Jul 26 '16 at 20:38
-
@twalberg I didn't know about `-S` or `-G` options of `git log`. Learned something today. Thank you. – axiac Jul 27 '16 at 09:12
1 Answers
git log -S
From git log --help
:
-S<string>
Look for differences that change the number of occurrences of the specified string (i.e. addition/deletion) in a file. Intended for the scripter’s use.
It is useful when you’re looking for an exact block of code (like a struct), and want to know the history of that block since it first came into being: use the feature iteratively to feed the interesting block in the preimage back into -S, and keep going until you get the very first version of the block.
git log --patch | git log -p
This shows the diff patch for each commit. The +
at the start of lines, highlighted green, are lines that were added. The -
at the start of lines, highlighted red, are lines that were removed.
Example: Combined Together:
Let's say I want to know the last time I used the python function subprocess.call()
. We can search for the string subprocess\.call\(
as the argument for the -S diff option like so:
git log -Ssubprocess\.call\( --patch
This use of git log will show the log for each commit that has the text change that you specified, and show the diff patches for each of those commits.
Bonus:
If I want to know how I used a function that no longer has anything calling it, I would simply:
text=subprocess\.call\(
git log --patch -S$text | grep -m 10 "$text"
or if I want to know the last time it was specifically removed:
text=subprocess\.call\(
git log --patch -S$text | grep -m 1 "^-.*$text"

- 5,777
- 1
- 15
- 27
-
Adding a file path at the end of the git log commands further narrow down file(s) to search through. – zaxishere Apr 29 '22 at 13:35