Git: Search in all versions of a file for a specific string

Hi,

to search in all versions of a file for a specfic string:
First get a list of all commits, then grep each commit:

export File=myFolder/myFile
for Hash in `git rev-list HEAD -- $File`; do
    git grep -i -F MySearch String $Hash:$File;
done

This will list all commits and the line where the string is found.

If you want to search the whole repository for a string simple omit the $File variable

for Hash in `git rev-list HEAD --`; do
    echo COMMIT $Hash
    git grep -i -F MySearchString $Hash;
done

Note: On older git versions and greater commits this sometimes raises a segmentation fault.

If you just want to search within a given time range use

export File=myFolder/myFile
for Hash in `git rev-list --after="2021-08-31" --until="2021-10-10" HEAD -- $File`; do
    git grep -i -F MySearch String $Hash:$File;
done

Michael

Leave a Reply