Git

How to use grep for searching in the git repository

Any git repository contains many files, folders, branches, tags, etc. Sometimes it requires searching the particular content in the git repository using a regular expression pattern. `git grep` command is used to search in the checkout branch and local files. But if the user is searching the content in one branch, but the content is stored in another branch of the repository, then he/she will not get the searching output. In that case, the user has to run the `git grep` command to apply the search in all repository branches.

Configuration parameters of grep command:

The `git grep` command parameters are used to configure this command have mentioned below.

Parameter Name Purpose
grep.patternType It is used to set the default matching behavior.
grep.fullName It is set to true for enabling the –full-name option by default.
grep.column It is set to true for enabling the –column option by default.
grep.lineNumber It is set to true for enabling -n option by default.
grep.extendedRegexp It is set to true for enabling the –extended-regexp option by default. But this option will not work if the grep. Pattern type contains another value in place of the default value.
grep. threads It is used to set the number of grep worker threads.
grep.fallbackToNoIndex If it is set to true, then the git grep –no-index when the git grep executed outside of a git repository. The default value of this parameter is false.

Options of grep command:

The `git grep` command has many options to search the repository content in different ways. Some of the commonly used grep options have described below.

Option Purpose
-i, –ignore-case It is used for case insensitive matches of the patterns and the files.
-I It is used to don’t match the pattern in binary files.
–max-depth <depth> It is used for each given on the command line. The depth value of -1 indicates no limit. This option is ignored if contains active wildcards.
-r, –recursive It works like –max-depth=-1, and it is the default value.
–no-recursive It works like –max-depth=0.
-w, –word-regexp It is used to match the pattern only at the word boundary.
-v, –invert-match It is used to select non-matching lines.
–full-name It is used to force the paths to the output relative to the project top directory.
-e It is used for the patterns starting with – and should be used with the grep.
–and, –or, –not, ( … ) These options are used to define the multiple patterns for searching. –or is the default operator and –and has higher precedence than –or.
-E, –extended-regexp, -G, –basic-regexp It is used for POSIX extended/basic regexp patterns.
-P, –perl-regexp It is used for Perl-compatible regular expression patterns.
-F, –fixed-strings It is used for the fixed string patterns.
-f <file> It is used to read the patterns from the file.
-n, –line-number It is used to prefix the line number to matching lines.
-o, –only-matching It is used to print only the matched (non-empty) parts of a matching line.
-c, –count It is used to show the number of lines that match.
–break It is used to print an empty line between the matches from the different files.
–help It is used to display all available options with the description of the grep command.

Enable grep configuration:

Before running the `git grep` command of this tutorial, run the following command to enable –extended-regexp and -n options of the grep command.

$ git config --global grep.extendRegexp true
$ git config --global grep.lineNumber true

Use of grep command for searching:

A local repository named book-store has been used in this tutorial to check the output of the grep command for searching content in the repository. The repository contains two files. These are booklist.php and booktype.php.

Run the following command to search the word ‘Book Type’ in the repository files.

$ git grep 'Book type' $(git rev-list –all)

The following output shows that the word ‘Book type’ exists in line 1 of the booktype.php file.

Run the following command to search the lines of the repository files with the commit SHA values that contain ‘boo’ at the starting of the files. Here, the -i option has used for case-insensitive search.

$ git grep -i 'boo*' $(git rev-list --all)

The following output shows that ‘boo’ contains two files at line number 1, but the entry for the booklist.php file has appeared two times for two commits.

The pattern has been searched inside the content of the repository file in the previous commands. Run the following command to search the content of the particular file.

$ git grep -f 'booktype.php.'

The following output shows that the booktype.php file exists in the current repository, and the file contains a single line.

Run the following command to search the pattern, ‘Book’ inside the content of the repository files. Here, the -e option has used for pattern matching.

$ git grep -e 'Book'

The following output shows that both booklist.php and booktype.php files contain the word ‘Book’ at line number 1.

Run the following command to search multiple patterns inside the content of the repository files. Here, the -E option has used for regex pattern matching, and the pipe (|) is working as logical OR. The files that contain the word ‘Book’ or ‘author’ will be shown after executing the following command.

$ git grep -E 'Book*|author.'

The following output shows that the word ‘author’ exists two times in the authorinfo.php file, and the word ‘Book’ exists one time in the booklist.php and booktype.php file.

Conclusion:

The `git grep` is a useful command for searching the specific content in the git repository. The searching can be done in different ways by using the different options of this command. The uses of some options have been described in this tutorial by using a demo repository.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.