How can I pass a string to git log -S instead of just a word?
Asked Answered
O

3

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.

Obduliaobdurate answered 4/12, 2015 at 20:36 Comment(3)
Weird, in a quick test, git log -S 'This repos' works for me. What version of Git are you using?Firedrake
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.Obduliaobdurate
I read git-log's doc but its meaning isn't totally clear to me: -S<string>: 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.Fivefold
T
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.

Tridactyl answered 11/1, 2016 at 10:19 Comment(0)
V
-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""
Volley answered 10/1, 2016 at 15:55 Comment(2)
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.Obduliaobdurate
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.Fredia
H
-1

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

Hospitalization answered 7/7, 2019 at 10:3 Comment(2)
That would be the same as git log -S "hello world" or git log -S"hello world" that I mention in my answer.Tridactyl
@Tridactyl you're right, I thought maybe the format of the other options is invalid, but after checking now, it does seem to be valid.Hospitalization

© 2022 - 2024 — McMap. All rights reserved.