How to grep the committed code on Git

Introduction to the grep command

Grep is a command-line tool that is used for searching through files and directories for a specific string of text or pattern. Similar to the grep command-line tool, the grep command can search for specific text patterns within the contents of committed code on Git.

Let's explore using the grep command in Git to search through committed code.

Steps to search committed code using the grep command

  1. Clone the repository.

  2. Switch to the repository directory.

  3. Search for the pattern in the committed code.

Step 1: Clone the repository

Before searching through the committed code on Git, we need to clone the Git repository.

git clone <repository url>
Cloning the repository
  • The <repository-url> is the URL of the Git repository that we want to clone.

Step 2: Switch to the repository directory

After cloning the repository, switch to the repository directory using the following command:

cd <repository-name>
Switching to the repository directory
  • The <repository-name> is the name of the directory that was created when we cloned the Git repository.

Step 3: Search for the pattern in the committed code

The syntax for using the grep command to search through committed code is similar to searching through files.

git grep <options> "pattern" <commit-id> <file-name>
Syntax of the grep command
  • <options>: This argument is optional and can be used to modify the behavior of the grep command.

  • pattern: This is the pattern we want to search in the committed code.

  • <commit-id>: This argument is optional and can be used to search the pattern through a specific commit.

  • <file-name>: This argument is optional and can be used to search the pattern through the specific file.

Options

Some of the useful options that can be used with the grep commands are:

Case sensitivity and insensitivity: By default, grep performs case-sensitive searches. To perform a case-insensitive search, use the -i option:

git grep -i "pattern"
Performing case-insensitive search

Displaying the matching part: Grep provides an option to display only the matching part of the line using the -o option:

git grep -o "pattern"
Command to display only the matching part

Displaying line number: To display the line number along with the matching line, use the -n option:

git grep -n "pattern"
Command to display the line number

Displaying count of lines: To display the count of lines that matches the patterns in the files, use the -c option:

git grep -c "pattern"
Command to display the count of matching lines

Displaying matches with a line break: To display the matches from all the files with a line break, use the --break option:

git grep --break "pattern"
Command to display matches with line break

Try it yourself

Follow the above steps in the terminal below to get hands-on practice using the grep command.

Try the above commands by cloning the official GitHub repository of scikit-learn library using the git clone https://github.com/scikit-learn/scikit-learn.git command.

We can use the git log command to list the commits that have been made to the repository.

Terminal 1
Terminal
Loading...

Conclusion

The grep command can be a powerful tool for searching through committed code in a Git repository. By following the above steps, we can quickly and easily search through committed code in our Git repository.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved