{"id":2674,"date":"2011-01-22T23:09:39","date_gmt":"2011-01-22T17:39:39","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=2674"},"modified":"2016-12-19T15:29:49","modified_gmt":"2016-12-19T09:59:49","slug":"git-grep","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/git-grep\/","title":{"rendered":"Git &#8211; Grep"},"content":{"rendered":"<p>Still in my early days of using Git.\u00a0Recently just to make sure that I have merged two branches \u00a0and have pushed changes to QA server successfully,I often used command &#8220;<span style=\"font-family: Consolas, Monaco, 'Courier New', Courier, monospace; font-size: 12px; line-height: 18px; white-space: pre;\">find | xargs grep<\/span>&#8221; .But to deal with this kind of situations Git provides &#8220;grep&#8221; command. Just as name suggests this commands searches for regex pattern passed to it.<\/p>\n<p>[shell] git grep &amp;quot;regex&amp;quot; [\/shell]<\/p>\n<p>Example &#8211; To find usage of word &#8220;collections&#8221; \u00a0in each file.<\/p>\n<p>[shell] git grep &#8211;ignore-case &amp;quot;collections&amp;quot;[\/shell]<\/p>\n<p>output :<\/p>\n<p>[shell]ReadMe.txt:groovy Collections<br \/>\nReadMe.txt:nice collections<br \/>\n[\/shell]<\/p>\n<p>Now this just described which of project files have word &#8216;list&#8217; in it and how many times.One can also use -c option to count occurrences \u00a0of word &#8220;collections&#8221;<\/p>\n<p>[shell]git grep &#8211;ignore-case -c &amp;quot;collections&amp;quot;[\/shell]<\/p>\n<p>Output:<\/p>\n<p>[shell]ReadMe.txt:2[\/shell]<\/p>\n<div id=\"_mcePaste\">\n<div>Now a question \u00a0why I am not using command (and favoring &#8220;git grep&#8221;)<\/div>\n<div>[shell]find | xargs grep -y &amp;quot;collections&amp;quot;[\/shell]<\/p>\n<\/div>\n<div>which will easily do the same for me and would perhaps list some extra \u00a0files (which are not exactly part of my app). Now here come the answer with &#8220;git grep&#8221; I can also search in previous versions of my project.<\/div>\n<div>Example<\/div>\n<div>[shell]git grep &#8211;ignore-case \u00a0&amp;quot;collections&amp;quot; ea145f1[\/shell]<\/p>\n<\/div>\n<div>output :<\/div>\n<div>[shell] ea145f1:ReadMe.txt:groovy Collections[\/shell]<\/p>\n<\/div>\n<div>It searches for word &#8220;collections&#8221; in my previous commits . (As second line in file ReadMe.txt was added after commit numbered ea145f1) .Simply awesome.And this is \u00a0not it. \u00a0To explore more visit. <a href=\"http:\/\/www.kernel.org\/pub\/software\/scm\/git\/docs\/git-grep.html\">git grep manual page<\/a> or the git community book which is maintained by Scott Chacon<\/div>\n<div>_________________________________<\/div>\n<div>\n<p><em>Hitesh Bhatia<\/em><a href=\"mailto:hitesh@intelligrape.com?subject=Feedback%20On%20Blog\"><\/a><\/p>\n<p>Mail,LinkedIn,<a href=\"http:\/\/www.facebook.com\/home.php?#%21\/profile.php?id=100000114437286\" target=\"_blank\">Facebook<\/a>,<a href=\"http:\/\/twitter.com\/d1_ricky\" target=\"_blank\">Twitter<\/a><\/p>\n<p>_________________________________<\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Still in my early days of using Git.\u00a0Recently just to make sure that I have merged two branches \u00a0and have pushed changes to QA server successfully,I often used command &#8220;find | xargs grep&#8221; .But to deal with this kind of situations Git provides &#8220;grep&#8221; command. Just as name suggests this commands searches for regex pattern [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[1],"tags":[375,503,459],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2674"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=2674"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2674\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=2674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=2674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=2674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}