Git – Grep

22 / Jan / 2011 by Hitesh Bhatia 0 comments

Still in my early days of using Git. Recently just to make sure that I have merged two branches  and have pushed changes to QA server successfully,I often used command “find | xargs grep” .But to deal with this kind of situations Git provides “grep” command. Just as name suggests this commands searches for regex pattern passed to it.

[shell] git grep "regex" [/shell]

Example – To find usage of word “collections”  in each file.

[shell] git grep –ignore-case "collections"[/shell]

output :

[shell]ReadMe.txt:groovy Collections
ReadMe.txt:nice collections

Now this just described which of project files have word ‘list’ in it and how many times.One can also use -c option to count occurrences  of word “collections”

[shell]git grep –ignore-case -c "collections"[/shell]



Now a question  why I am not using command (and favoring “git grep”)
[shell]find | xargs grep -y "collections"[/shell]

which will easily do the same for me and would perhaps list some extra  files (which are not exactly part of my app). Now here come the answer with “git grep” I can also search in previous versions of my project.
[shell]git grep –ignore-case  "collections" ea145f1[/shell]

output :
[shell] ea145f1:ReadMe.txt:groovy Collections[/shell]

It searches for word “collections” in my previous commits . (As second line in file ReadMe.txt was added after commit numbered ea145f1) .Simply awesome.And this is  not it.  To explore more visit. git grep manual page or the git community book which is maintained by Scott Chacon

Hitesh Bhatia




Tag -

git github grep

Leave a Reply

Your email address will not be published. Required fields are marked *