Consider this scenario:
- You want to delete a method
foothat seems to be no longer used.
- You’re a bit nervous because you work in a dynamic language (like Ruby), so you can’t rely on grep to find all usages of this method.
- You wonder, why did this method ever exist?
git blameisn’t very helpful - just shows the last change to that method, which isn’t relevant
git log -S to the rescue
From the git-log docs:
-S <string> Look for differences that change the number of occurrences of the specified string (i.e. addition/deletion) in a file.
So git will spit out all of the commits that either deleted or added the given string (e.g. calls to a method)
Even more powerful: git log -G
-G<regex> Look for differences whose patch text contains added/removed lines that match <regex>.
These two commands have become indispensable tools for me, especially when navigating an old codebase with a lot of crufty code.