Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git log - find last use of function

Tags:

git

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.

like image 658
Daniel Larsson Avatar asked Jul 26 '16 19:07

Daniel Larsson


People also ask

How do I search for a string in git log?

Simply run git log with the -L option, and it will show you the history of a function or line of code in your codebase.

How do I see my git log history?

`git log` command is used to view the commit history and display the necessary information of the git repository. This command displays the latest git commits information in chronological order, and the last commit will be displayed first.

How can I see my last commit?

If you want to see what's happened recently in your project, you can use git log . This command will output a list of the latest commits in chronological order, with the latest commit first.

What is git Shortlog?

The git shortlog command is a special version of git log intended for creating release announcements. It groups each commit by author and displays the first line of each commit message. This is an easy way to see who's been working on what.


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"
like image 199
Bryce Drew Avatar answered Oct 17 '22 21:10

Bryce Drew