| by Arround The Web | No comments

How to Search in Commit Messages Using Command Line?

While working on a large project, developers make many commits that they need later. However, when they try to find a particular commit in history, it becomes hard to find because the repository contains hundreds of commits. In this situation, Git allows filtering out the specific commit from the commit history.

This write-up will explain the various methods to search in commit messages using the command line.

How to Search in Commit Messages Using Command Line?

Git provides different methods to search for commit messages using the command line, such as:

    • Search Commit Message Through Case-sensitive Words
    • Search Commit Message Through Case-insensitive Words
    • Search Commit Message Through Multiple Words Entire History
    • Search Commit Message Through Restricted Words

Method 1: How to Search Commit Messages Through Case-sensitive Words?

To search commit messages through case-sensitive words, follow the provided steps.

Step 1: Navigate to Local Git Repository

First, use the below stated command and switch to the desired directory:

$ cd "C:\Git\Repo1"

 
Step 2: Check Git Log

Then, view the commit history by checking the Git log with the help of the following command:

$ git log --oneline

 
The below output displays the list of all commits made in the repository:


Step 3: Perform Case-sensitive Search

To perform a case-sensitive matching, run the “git log” command with the “–grep=<keyword>” option:

$ git log --oneline --grep="file"

 
Here, the “–grep” option is used to search the whole commit message:

Method 2: How to Search Commit Messages Through Case-insensitive Words?

For performing a case-insensitive search, utilize the below-provided command with the “-i” option:

$ git log --oneline --grep="file" -i

 
The above-stated command will provide the result of searched word “file” including lowercase and uppercase words:

Method 3: How to Search Commit Messages Through Multiple Words Entire History?

Specify the “–grep” flag multiple times to perform a multiple-word matching. This will display the commit messages that match at least one time with the specified word:

$ git log --oneline --grep="file" --grep="demo"

 
The output below displays the commit messages that have both “file” and “demo” words in them:

Method 4: How to Search Commit Messages Through Restricted Words?

Use the “–all-match” option with the below-listed command to limit the searches to ones that match all the provided words:

$ git log --oneline --grep="file" --grep="demo" --all-match

 
According to the below output, the provided command has displayed only those results that matched to commit messages containing both “file” and “demo” words:


We have explained the several methods for searching in commit messages using the command line.

Conclusion

There are various methods available to search in commit messages using the command line, such as the “git log –oneline –grep=<keyword>” command is used to perform the case-sensitive search, and the “-i” flag with the previous command performs case-insensitive matching. To perform a multiple-word matching, specify the “–grep” flag for multiple words and utilize the “–all-match” option to limit the searches to ones that match all the provided words. This write-up described the different methods to search commit messages in Git using the command line.

Share Button

Source: linuxhint.com

Leave a Reply