A Very Useful Git Command For Searching Text

A Very Useful Git Command For Searching Text

Written by Yogesh Chavan on Jul 17th, 2021 Views Report Post

Table of contents

In this article, we will see a very useful command to search for a particular text inside your project.

Many times when working on code, you want to find out where in the repository a particular text is used, either to replace it with other text or for debugging purpose.

If you try to use a global search in your favorite IDE like Visual Studio Code or Sublime Text, you will get the text you are searching in hundreds or thousands of files like package.json, package-lock.json, and other un-necessary files.

Instead, you can use git grep command to quickly search within files. So let’s dive into it.


Suppose, you want to find the files in which a particular statement like require("express") or require('express') is used, just using a global search for express in VS Code will give many results, instead we can run the following command from a terminal in your repository.

git grep -n $'require([\'"]express[\'"]'

The above command will list out the filename, line number along with the matching text from that file

This will produce the output as shown below:

require


Suppose you want to find in which files, a particular property like http_mode from app object is used.

In short, if you want to find the occurrence of .http_mode you can execute the following command:

git grep $'\.http_mode'

This will produce the output as shown below:

git_grep


If you want to find the text NavLink only in a particular directory instead of all the directories, you can execute the following command:

grep -rn src/routes -e NavLink

This will produce the output as shown below:

NavLink

Conclusion

As you have seen, using simple git and grep combination you can quickly find the required text instead of looking through thousands of files in your IDE search.

Thanks for reading!

Check out my recently published Mastering Redux course.

In this course, you will learn:

  • Basic and advanced Redux
  • How to manage the complex state of array and objects
  • How to use multiple reducers to manage complex redux state
  • How to debug Redux application
  • How to use Redux in React using react-redux library to make your app reactive.
  • How to use redux-thunk library to handle async API calls and much more

and then finally we'll build a complete food ordering app from scratch with stripe integration for accepting payments and deploy it to the production.

Want to stay up to date with regular content regarding JavaScript, React, Node.js? Follow me on LinkedIn.

Comments (0)