Search code examples
git

Search all files of all commits in git log for specific string


Assume I have a git repo with various commits, each of which did modify/add/delete different files.

I am looking for a way to search through all files of all commits for a specific string. For example, assume my repo had two files and looks like this and I am searching for the string test:

1 commit: added file A      "file A" does not contain string "test"
2 commit: modified file A   "file A" does not contain string "test"
3 commit: added file B      "file A" and "file B" do not contain string "test"
4 commit: modified file B   "file B" now contains string "test", "file A" not
5 commit: deleted file A    "file B" still contains string "test"
6 commit: modified file B   "file B" does not contain string "test"

In this case, the command should return the SHAs of commits 4 and 5, because in this state, there existed files in the workspace that contained the search string test.

Is this possible? I am looking for either a solution in git directly or in Tortoise git. I already tried to search in the log of Tortoise git, but without any success, apparently, the search there doesn't search the file content.


Solution

  • You are looking for

    git log -Gtest --all
    

    -G lists commits whose modifications contain the given text (regular expression).