Hello everyone, today I will introduce you to a hidden function – search.

One of the things we often encounter when writing code is that we want to know how a function is defined so that we know how to call it. If the code is small, we can certainly search for human flesh, but if the code is large, human flesh search obviously not. If you’re using an IDE, and today’s ides generally inherit this functionality, it might be a bit of a hassle if, like me, you prefer an editor to an IDE.

You can use git grep to help you find the code.

Code search

Let’s say we’re in a Python repository and want to know where get_yaml_config actually appears, so we can find its definition and parameters.

At this point we can execute from the command line:

git grep get_yaml_config

Copy the code

After that, we will enter a Vim screen that shows all the locations of this function.


We can obviously see where def appears in the function definition, so we just need to look for this line of code in the file.

This is the basic git grep usage, and you might say that this is still a very inconvenient way to find a file, but if the file is very large, you still have to dig through it line by line. Is there a way to show the exact location as well?

Of course there is, and it’s easy, we just need to add -n.

git grep -n get_yaml_config

Copy the code

Now we find that we have the line number with us.

Of course it does more than that, sometimes we just find the specific location is not useful, we also want to know what the function is called. We can do that, just by adding a -p argument.


It looks like the result is similar, but it shows the function at each call location.

Not only that, git grep can also look up code from a previous version. If you were using the IDE’s lookup tool, you would have to checkout to the previous version and then do the lookup. Using Git grep eliminates this step.

The method used is very simple, we just need to add git commitid or tag at the end of it.

git grep -n -p get_yaml_config 9caf1ecda6

Copy the code

Log search

Sometimes we don’t want to know where the function is, but rather when it was added, or when a change was made. Git can still be used to search not only code but also logs.

Git log is also a simple way to use git log by adding -l and get_yaml_config:generate_create_sql.py. This represents a change to the get_yaml_config function in the generate_create_sql.py file.

git log -L :get_yaml_config:generate_create_sql.py 

Copy the code

The result would be:


It shows when the code changed, the corresponding COMMIT, and even the specific code and the modifier. Git log: git log, git log, git log

At first, it will be a little uncomfortable to use git and feel that it is very troublesome to type commands. However, after using git more, you will find that the search speed is very fast and the memory consumption is also less. For example, after the number of files opened in IDE, it is very slow to search and need to wait for a long time. In this case, you can try using Git instead, the experience is really not the same, I strongly recommend it.

That’s all for today’s article. I sincerely wish you all a fruitful day. If you still like today’s content, please join us in a three-way support.

Original link, ask a concern