Daniel Larsson Daniel Larsson - 1 year ago 88
Git Question

Git log - find last use of function

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.

Answer Source

git log -S

From git log --help:


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.


If I want to know how I used a function that no longer has anything calling it, I would simply:

git log --patch -S$text | grep -m 10 "$text"

or if I want to know the last time it was specifically removed:

git log --patch -S$text | grep -m 1 "^-.*$text"