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.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.
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.
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.
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.
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.
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.
from Linux Hint https://ift.tt/34DQI4y
0 Comments