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.

 git grep "regex" 

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

 git grep --ignore-case "collections"

output :

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”

git grep --ignore-case -c "collections"

Output:

ReadMe.txt:2
Now a question  why I am not using command (and favoring “git grep”)
find | xargs grep -y "collections"
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.
Example
git grep --ignore-case  "collections" ea145f1
output :
 ea145f1:ReadMe.txt:groovy Collections
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

Mail,LinkedIn,Facebook,Twitter

_________________________________

Tag -

git github grep
FOUND THIS USEFUL? SHARE IT

Leave a comment -