549

I want to be able to find a certain string which was introduced in any commit in any branch, how can I do that? I found something (that I modified for Win32), but git whatchanged doesn't seem to be looking into the different branches (ignore the py3k chunk, it's just a msys/win line feed fix)

git whatchanged -- <file> | \
grep "^commit " | \
python -c "exec(\"import sys,msvcrt,os\nmsvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)\nfor l in sys.stdin: print(l.split()[1])\")" | \
xargs -i% git show origin % -- <file>

It doesn't really matter if your solution is slow.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Jonas Byström
  • 25,316
  • 23
  • 100
  • 147
  • Closely related: [How to grep git commits for a certain word](http://stackoverflow.com/q/1337320/456814). –  Jun 12 '14 at 21:15

7 Answers7

971

You can do:

git log -S <search string> --source --all

To find all commits that added or removed the fixed string search string. The --all parameter means to start from every branch and --source means to show which of those branches led to finding that commit. It's often useful to add -p to show the patches that each of those commits would introduce as well.

Versions of git since 1.7.4 also have a similar -G option, which takes a regular expression. This actually has different (and rather more obvious) semantics, explained in this blog post from Junio Hamano.

As thameera points out in the comments, you need to put quotes around the search term if it contains spaces or other special characters, for example:

git log -S 'hello world' --source --all
git log -S "dude, where's my car?" --source --all

Here's an example using -G to find occurrences of function foo() {:

git log -G "^(\s)*function foo[(][)](\s)*{$" --source --all
Black
  • 18,150
  • 39
  • 158
  • 271
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • 33
    +1 for excellence. Pointing at -S is one thing, explaining things, better. Also, I like to use --decorate to see what branches things come from – sehe Apr 28 '11 at 09:25
  • 9
    @sehe: Thanks for your nice comment. I guess it's worth noting that `--decorate` only adds the branch name to the commit at the tip of each branch. In practice I don't really use `--source` or `--decorate`, and instead use `git branch -a --contains ` to find which branches contain the commit I'm interested in. – Mark Longair Apr 28 '11 at 09:40
  • 7
    add -p to see the inline diff, as well, FWIW – rogerdpack Sep 05 '14 at 16:16
  • 1
    @MarkLongair it doesn't show the changes made in merge. Any suggestion to show those as well? – Pahlevi Fikri Auliya May 07 '15 at 07:55
  • 4
    For me this only works if I **remove the space** between the -S and the search term, i.e., `git log -S"dude, where's my car?" --source --all`. @ribamar also wrote that in an [answer](http://stackoverflow.com/a/35988667/1330329) below, but it might easily get overlooked next to this top answer. – bug313 Jan 13 '17 at 14:01
  • @bug313 I can't reproduce that - can you let me know what version of Git you're seeing that problem with? (i.e. what is the output of `git --version`?) I see that the documentation prefers the form with no space, so perhaps I should change the answer to that form anyway, but it's a bit less clear to read. – Mark Longair Jan 22 '17 at 19:26
  • @MarkLongair I just tried it again, your solution works on my machine with git version 1.7.9.5, however it does not work on another set up which uses version 1.7.1 – bug313 Jan 24 '17 at 12:26
  • continued from above comment: `$ git log -S 'hello world' --source --all` returns: "fatal: ambiguous argument 'hello world': unknown revision or path not in the working tree. Use '--' to separate paths from revisions" Considering that this version is several years old and the current release is 2.11, it might be the time for our sysadmin to update it, but nevertheless it does work without the space. – bug313 Jan 24 '17 at 12:35
  • @MarkLongair, what type of regex does git use? is it extended regex, or basic regex or pcre? – alpha_989 May 08 '18 at 14:45
  • Note: If a line removes and adds the same substring, it won't show up in this search (i.e. it seemingly uses the actual word diff rather than the line diff). Anyone know how to use the line diff? – DylanYoung Nov 26 '18 at 19:54
  • This is not what OP is asking. This answer actually does: https://stackoverflow.com/a/31621921/1525495 – Jorge Fuentes González Feb 22 '21 at 10:52
  • To see the actual changes you can append `--patch`. – ccpizza Apr 11 '21 at 15:48
114

--reverse is also helpful since you want the first commit that made the change:

git log --all -p --reverse --source -S 'needle'

This way older commits will appear first.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
31

Messing around with the same answers:

$ git config --global alias.find '!git log --color -p -S '
  • ! is needed because other way, git do not pass argument correctly to -S. See this response
  • --color and -p helps to show exactly "whatchanged"

Now you can do

$ git find <whatever>

or

$ git find <whatever> --all
$ git find <whatever> master develop
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
albfan
  • 12,542
  • 4
  • 61
  • 80
23

Mark Longair’s answer is excellent, but I have found this simpler version to work for me.

git log -S whatever
Community
  • 1
  • 1
Zombo
  • 1
  • 62
  • 391
  • 407
  • 26
    Just to clarify, that works fine if the commit you're looking for is in `HEAD`, but this particular question asked specifically about looking across all the branches in a repository. – Mark Longair Aug 01 '13 at 09:21
9
git log -S"string_to_search" # options like --source --reverse --all etc

Pay attention not to use spaces between S and "string_to_search". In some setups (git 1.7.1), you'll get an error like:

fatal: ambiguous argument 'string_to_search': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
ribamar
  • 1,435
  • 1
  • 16
  • 26
6

While this doesn't directly answer you question, I think it might be a good solution for you in the future. I saw a part of my code, which was bad. Didn't know who wrote it or when. I could see all changes from the file, but it was clear that the code had been moved from some other file to this one. I wanted to find who actually added it in the first place.

To do this, I used Git bisect, which quickly let me find the sinner.

I ran git bisect start and then git bisect bad, because the revision checked out had the issue. Since I didn't know when the problem occured, I targetted the first commit for the "good", git bisect good <initial sha>.

Then I just kept searching the repo for the bad code. When I found it, I ran git bisect bad, and when it wasn't there: git bisect good.

In ~11 steps, I had covered ~1000 commits and found the exact commit, where the issue was introduced. Pretty great.

Eldamir
  • 9,888
  • 6
  • 52
  • 73
5

Not sure why the accepted answer doesn't work in my environment, finally I run below command to get what I need

git log --pretty=format:"%h - %an, %ar : %s"|grep "STRING"
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
BMW
  • 42,880
  • 12
  • 99
  • 116