34

I have a Git repository that contains hundreds of commits and several branches. How to search a particular commit that contains a certain string e.g. "helper function"? Ideally, the string can be denoted by a regex.

knittl
  • 246,190
  • 53
  • 318
  • 364
moey
  • 10,587
  • 25
  • 68
  • 112

2 Answers2

39

Newer versions of Git support git log -G<regex>:

git log -G'helper.*function' --full-history --all

This command will search for the regex in the diff of each commit, and only display commits which introduced a change that matches the regex.

knittl
  • 246,190
  • 53
  • 318
  • 364
  • 1
    This was the answer I was looking for, and how I understood the question was written. – Eosis Apr 07 '20 at 09:48
33

Credits go to this answer:

git log --all --grep='Build 0051'

# case insensitive
git log --all --grep='Build 0051' -i
Community
  • 1
  • 1
moey
  • 10,587
  • 25
  • 68
  • 112
  • 5
    This looks for the regex in the commit message as opposed to knittl's answer, where -G searches through the commit's diff. – staafl Jan 16 '18 at 20:09