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.
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.
`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.
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.
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.
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.
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.
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:
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With