Git search for string in a single file's history

So if I have a file called foo.rb and it is giving me an error for a missing method called bar, so I want to search the history of foo.rb for the string bar to see if it was ever defined in the past.

I found this Search all of Git history for a string?

But this searches all files. I just want to search in one file.


For this purpose you can use the -S option to git log:

git log -S'bar' -- foo.rb

Or maybe you can try this one (from related questions Search all of git history for string)

git rev-list --all foo.rb | (
    while read revision; do
        git grep -F 'bar' $revision foo.rb
    done
)

It will actually look for file content and not commit messages/patches for any occurence of bar.


There's an override for git log command (from manual):

$ git log Makefile      # commits that modify Makefile

So you could use:

git log foo.rb | grep "bar"

I used git log -S "string" --follow -p "path of file" which shows the full history of all changes with that string.