How to Search Commit Messages Using Command Line in Git

John Wachira Feb 15, 2024
  1. Search Commit Messages Using Command Line
  2. Conclusion
How to Search Commit Messages Using Command Line in Git

You can format your git log command to display commit with a commit message matching a specified pattern. This makes it easier when you want to find a commit, but your repository has hundreds of commits.

This article will discuss the process of searching through your commits history and filtering by commit message.

Search Commit Messages Using Command Line

Although using the git log command with the --oneline flag makes it easier to view our commit history, as we can see below.

View commit history using git log command

This list goes on since we have over 200 commits in this repository, and skimming through the list will take time. We can simplify it by formatting the git log command to filter out based on our needs.

In our case, we want to display commit whose commit message has Update. We can run the git log command as shown below.

Command

$ git log --grep=Update

The above command will display any commit whose message matches the pattern Update.

Output:

Use git log command to display commit message that has Update

Our repository still has a dozen of commits whose message has Update in them. We can add the --oneline flag to simplify the output.

Command:

$ git log --grep=Update --oneline

Output:

Add –oneline flag to simplify the display of commit messages

It is as simple as that. Let’s quickly check out other filtering options in Git.

Filter Commit History by Author

You can filter your commit history by author with the git log command.

Command:

$ git log --author="Authors-name"

Filter Commit History by Content

You can use git log to search for commits whose changes introduced or removed a specific pattern in a line of code.

Command:

$ git log -S"Content"

Now, let’s have an example where we’ll attempt to search for commits that introduced or removed the phrase API in a line of code.

Command:

$ git log -S"API" --oneline

Again, we have included the --oneline option for a simpler display.

Output:

search for commits that introduced or removed the phrase API in a line of code

Conclusion

Git allows us to filter out the output from the git log commands when we want to search through our commit history. Also, we can make the output in displaying the commit history much simpler.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Commit