1

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?

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
Khushal
  • 123
  • 1
  • 4

1 Answers1

3

I don't know about Jupyter Notebook, but use the terminal. Make sure you have already git cloned 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:

  1. Random experience, Googling, and talking to people over the years
  2. Read the man (manual) pages:
  3. man git grep
  4. man grep
  5. man find
  6. https://regex101.com/

Related:

  1. GitHub: How to do case sensitive search for the code in repository?
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
  • Does `git grep -n` search all branches? Or only the current working branch? – John Sep 30 '22 at 02:31
  • @John, by default it only searches the working branch--whatever is on your active file system ("working tree") at the moment, and tracked by git, I believe. – Gabriel Staples Sep 30 '22 at 07:12
  • Does the aforementioned command search the file which has already been deleted in a specific commit? – John Sep 30 '22 at 07:14
  • @John, if it's been deleted, it's no longer in your working tree (what you see in your file system via `ls` or in a file explorer GUI), so no, it won't search it. – Gabriel Staples Sep 30 '22 at 07:17
  • If the deleted file still lies in the repertory(I mean specific files were deleted by some commit, you can still restore the deleted files by `git`), is it possible to search the entire repertory? – John Sep 30 '22 at 07:43
  • @John, to search through a commit or list of commits, you can do this: `git grep -n "some regex search string" commit1 commit2 commit3 commit4`. To search for *changes* of a word, you can do this: [`git log -S "some string"` or `git log --all -S "some string"`](https://stackoverflow.com/a/54567708/4561887). I don't know how to do exactly what you've asked for, though, so I posted a new question 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](https://stackoverflow.com/q/73930303/4561887) – Gabriel Staples Oct 03 '22 at 01:08
  • @John, you can also try this to search for a match in your history like this: `git log -p`, then press the `/` key, type your regular expression search string, and press `Enter`. Press `n` for "next match" or `Shift` + `n` for "previous match". The `-p` in `git log` shows the changes in "patch" format for each commit. – Gabriel Staples Oct 03 '22 at 01:12
  • @John, after much effort I figured it out. See my full answer 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](https://stackoverflow.com/a/73931035/4561887) – Gabriel Staples Oct 03 '22 at 04:19