Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git search for string in a single file's history

Tags:

git

People also ask

How do I search text history in git?

Line Log Search 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.

How do I search for a string in git log?

Simple! Drop the 'log' argument. This will show the commit ID and line number of all instances of your string. (The string can also be a regular expression, which is handy if you know regex, too.)

What is git grep?

`git grep` command is used to search in the checkout branch and local files. But if the user is searching the content in one branch, but the content is stored in another branch of the repository, then he/she will not get the searching output.


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.