What git grep is and what it does
Git grep is a tool for searching through files in a git repository, looking for lines matching a given regular expression. If you only need to search for the first occurrence of the match, then this is the command you want. You can use any of the normal git commands to search in a particular directory or across all repositories, and grep will find the first match it finds. If files don’t exist, Git will create them for you. If a line already exists, it’s simply added to the end of what’s already there, rather than replacing it.
The source for this is from:
“DaveM.: Sorry for the confusion, but I thought you were just talking about the ‘git grep’ command, and asking which one to use when finding a specific file in a repo or using that command to search all files in all repos.”
There was miscommunication. I was not just asking which “git grep” to use, but I was asking:
Which command should I use when finding a specific file in a repo?
Which command should I use to search all files in all repos?
The answer (for both): “git grep” – but only if you’re looking for the first occurrence of the match. If you’re looking for every instance of the match, then “find” is better. Both commands will find each occurrence in a file.
There is also a “grep” command found under “Find” within “Git”, which is the same as using “git grep”.
How to use git grep to search for files and lines of code
git grep looks for the given regular expression in files. If a file doesn’t exist, it is created. If a line already exists, the line is simply added to what’s already there. If a line doesn’t exist after all files are checked, git grep exits with an error. If no regular expression is given, then everything will be examined verbatim–the entire contents of every file from all git repositories will be searched for occurrences of the given pattern. Here are the examples, each of which searches for a single regular expression.
# Search all files known to git for the given regular expression. The results are written to stdout.
git grep pattern
# Search all files known to git for the given regular expression, and don’t print anything if there are no matches. The results are written to stdout only if there is a match.
git grep –quiet pattern
# Same as above, but search only in the files in work tree (not including previous commits).
git grep –git-dir . pattern
# Same as above, but search only in the files at the top level of the current branch.
git grep pattern
The benefits of using git grep
Git grep is really useful if you want to know what kind of code is in a file, particularly if you’re looking for something specific. For example, you might want to know all the files which contain a certain variable definition. Just run git grep variable and then see which files come out: # git grep variable *
and then see which files come out: # and then make a diff to show the difference: # git log –oneline –graph –decorate=short ‘*’ | git diff-index -v
(Note that you will most likely want to use sed or awk instead of grep in this case.)
Git grep is also useful if you want to see what certain directories contain. A classic way of doing this is by using find to list all files in all directories under history. But git grep is a more compact approach. Here is an example:
# git grep -lRv bin/ * bin/add-cvs-id.sh bin/am-protocol.sh …
If you want to see ALL the patterns, you can use the following command:
Examples of how git grep can be used in your workflow
The following example creates a new array and then prints the file names of all lines containing the word array in any file that is under git control. The following example shows how to run this particular command: $ git grep -l array README.md
By default, git grep will search each file in the repository that you specify in the ‘grep’ command.
For example, to search all files under git control for a line containing “array”, you can run this command: $ git grep -l array README.md There are many additional arguments available for ‘git grep’. For more information about these options, read the (short) manpage for ‘git grep’.
The following example creates a new array and then prints the file names of all lines that contain the word array in a file not under git control. $ git grep -l -f /etc/hosts array This is because ‘git grep’ only searches files under git control by default.