Searching git repo for source-code change

Today I wanted to search for a Java class in my git repository that no longer exists. I can recall that I had used some code snippets in a class that no longer exists, but I cannot recall the class name, and I cannot recall which commit message I used to remove the class from the repository. My question was: Can I full-search my git repo?

Answer: Yes. It is possible to search the whole git for a regular expression. Let’s say I knew that in the class I was searching, I had the following code snippet “Jwts.builder()”, so I can search the whole repository for this snippet:

git grep "Jwts.builder()" $(git rev-list --all)

The result I get is a list of all files where the search string exists, and the git commit hash. With this you can checkout the revision (git commit hash), search for the file, and then continue to navigate from there on using your favorite IDE.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.