Issue
I am new to Github and Jupyter notebook.
I want to find a specific function in a git repository, how do I do it?
I have come to know that I could use Git grep, but I don't know the command.
Also, can I use Jupyter notebook to do this or do I have to use the terminal?
Solution
I don't know about Jupyter Notebook, but use the terminal. Make sure you have already git clone
d the repository and are inside its directory in the terminal. Then do git grep
from the terminal.
Search for text within files:
1. With git grep
(you must be inside a git repo):
Case-sensitive search. The -n
shows the line number where the result is found too.
git grep -n "my regular expression search string"
Case insensitive search (add -i
here):
git grep -ni "my case insensitive regular expression search string"
Find a function usage (left parenthesis below is optional)
git grep -n "myFunc("
Grep does regular expression search. Google it for details. Regular expressions are a very powerful way to do very specific string matching. As you learn the basics, practice testing and checking your regular expression (regex) searches with this tool here: https://regex101.com/.
For additional git grep
usage and commands, see my Q&A here: How to git grep
through a range of commits, all commits from the current commit back to the parent commit, or through all commits in entire repo.
2. With regular grep
(works anywhere, but operates about 100x slower than git grep
):
Same as above, except add -r
for 'r'ecursive (to search into directories). If you want to follow symbolic links too, use -R
instead of -r
.
Examples:
Case-sensitive search. The -n
shows the line number where the result is found too.
grep -rn "my regular expression search string"
Case insensitive search (add -i
here):
grep -rni "my case insensitive regular expression search string"
Find a function usage (left parenthesis below is optional)
grep -rn "myFunc("
Find a function (following symbolic links: add -R
):
grep -Rn "myFunc("
3. [RECOMMENDED] With ripgrep (rg
) (works anywhere, and is FASTER than both grep
AND git grep
, which is incredibly impressive!)
THIS IS THE BEST AND FASTEST OPTION!
Get ripgrep here: https://github.com/BurntSushi/ripgrep. Ex: to install on Linux Ubuntu 18.10 or later, use this installation command:
sudo apt update && sudo apt install ripgrep
Example Uses:
Case-sensitive search.
rg "my regular expression search string"
Case insensitive search (add -i
here):
rg -i "my regular expression search string"
Follow symbolic links with -L
:
rg -L "my regular expression search string"
Note that ripgrep automatically shows line numbers (-n
) when searching in a terminal, so there is no need to add -n
. It is on by default!
Bonus: find a file by name:
Just pipe the output of find
to be the input to grep
with the pipe operator (|
):
find | grep -ni "my_file_name"
Or, use -L
with find
to follow symbolic links:
find -L | grep -ni "my_file_name"
Notice the -i
above is for case-insensitive filename searches. Remove it to match the case too.
References:
- Random experience, Googling, and talking to people over the years
- Read the man (manual) pages:
man git grep
man grep
man find
- https://regex101.com/
Related:
Answered By - Gabriel Staples Answer Checked By - Willingham (WPSolving Volunteer)