Viewing all changes to a Java method using Git

First off, add this to your project's .gitattributes file:

*.java diff=java

Now, you're all set, you can now pass -L :<method>:file to git log to see all changes related to a Java method.

Here I want to see all changes related to the toString() method inside

git log -L :call:./core/src/java/org/apache/solr/servlet/

git log java method

Cool, right?

Other languages

To get this magic shining for other languages than Java, check out

man gitattributes

and the section Defining a custom hunk-header, there you'll see a list of languages you can add to .gitattributes as:


If your language is not listed there, you can write your own

[diff "foo"]

function to tell git log how to identify a method (the man page shows an example of this).

Happing code searching!

Licensed under CC BY Creative Commons License ~ ✉ torstein.k.johansen @ gmail ~ 🐘 ~ 🐦 @torsteinkrause