7

I wanted to search for the commit that either contains the string "hello world" or any commit that included that phrase inside of a file.

git log -SHello World doesn't work

git log -S'Hello World' doesn't work

git log -S"hello world" doesn't work

I have been using: git log -i --grep='hello world' but this one only works for commit messages.

VaTo
  • 2,936
  • 7
  • 38
  • 77
  • 1
    Weird, in a quick test, `git log -S 'This repos'` works for me. What version of Git are you using? – Whymarrh Dec 04 '15 at 21:00
  • 1
    I mean when I said "it doesn't work" I mean I didn't get any results back when I know that I do have that phrase in my log comments. – VaTo Dec 04 '15 at 21:35
  • I read [git-log's doc](https://www.kernel.org/pub/software/scm/git/docs/git-log.html) but its meaning isn't totally clear to me: *-S: Look for differences that change the number of occurrences of the specified string (i.e. addition/deletion) in a file. Intended for the scripter’s use. It is useful when you’re looking for an exact block of code (like a struct), and want to know the history of that block since it first came into being: use the feature iteratively to feed the interesting block in the preimage back into -S, and keep going until you get the very first version of the block.* – Nayuki Dec 05 '15 at 01:40

3 Answers3

8

The pickaxe search (git log -S) looks for the addition or removal of a word in a commit (and not in a commit message).
See the comment on this answer.

If you have a commit message which includes that word, but the commit content itself does not have "hello world" as being added or removed (meaning, for instance, it is already there, from previous commits), that commit would not be reported.

Other than that, git log -S 'This repos' or git log -S'This repos' should work just fine.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

How about the command git log "-Shello world"?

Sagie
  • 243
  • 1
  • 10
  • That would be the same as `git log -S "hello world"` or `git log -S"hello world"` that I mention in my answer. – VonC Jul 07 '19 at 10:46
  • @VonC you're right, I thought maybe the format of the other options is invalid, but after checking now, it does seem to be valid. – Sagie Jul 07 '19 at 11:16
-1

I wanted to search for the commit that either contains the string "hello world"

To search commits its pretty simple:

git log | grep "new image"

or any commit that included that phrase inside of a file. For this you will need to loop on each commit, check it out and then search for the give string.

For this kind of task you will need to use git filter-branch ...

# you will need something like:
git filter-branch --tree-filter "
      find . 
      -type f 
      -exec grep -rnw '/path/to/somewhere/' -e "pattern""
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • I feel like this should have a pretty straightforward command like git log -S or something similar and not write like a script just to search for a phrase. – VaTo Feb 29 '16 at 20:22
  • There seems to be so much wrong with this. First reflog complains about the newlines in the filter command, then there is a missing `\;` for the find command, then pattern should be in single quotes, because the whole command is already in whole quotes, meaning pattern is effectively unquoted. And last reflog actually seems to do some rewriting which is just bad, even if it isn't changing anything ... Problem for me is, that `git log -S'pattern'` doesn't seem to find all commits, not even the ones I KNOW matches the pattern. It's frustrating. – mxmlnkn Mar 14 '18 at 18:28