594

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.

1

7 Answers 7

1060

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 
Sign up to request clarification or add additional context in comments.

12 Comments

+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: 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 <commit-hash> to find which branches contain the commit I'm interested in.
add -p to see the inline diff, as well, FWIW
@MarkLongair it doesn't show the changes made in merge. Any suggestion to show those as well?
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 below, but it might easily get overlooked next to this top answer.
|
127

--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.

3 Comments

Is there a way to make the command stop after the first match is found?
@CorneliusRoemer maybe git log -n1?
@CiroSantilliOurBigBook.com nope doesn't seem to work with --reverse. only shows the latest commit
34

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 

Comments

24

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

git log -S whatever 

1 Comment

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.
10
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 

Comments

7

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" 

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.