Sling Academy
Home/DevOps/How to filter commits by message in Git log

How to filter commits by message in Git log

Last updated: January 27, 2024

Introduction

Git is a powerful version control system used by developers across the globe to track changes in source code throughout the software development lifecycle. One handy feature of Git is its ‘log’ command, which allows users to list the project history, filter it, and search for specific changes. This tutorial focuses on filtering commit messages within the git log to improve the performance of searches and review processes.

By the end of this guide, you will have a solid understanding of how to find commits in a Git repository based on the commit message, using different techniques and patterns.

Basic Usage of Git Log to Find Commits by Message

One of the simplest ways to filter commits by message is to use the basic ‘grep’ option. This command filters the output to show only commits with messages that match the specified pattern.

git log --grep="pattern"

For example, to find commits that have the word ‘bugfix’ in their message, you can use the following command:

git log --grep="bugfix"

Output:

commit 4da45bef... Author: Alex Doe <[email protected]> Date: Wed Sep 2 17:50:30 2021 -0300

    Bugfix: resolve issue with user login

commit 7fbd9dc5... Author: John Smith <[email protected]> Date: Mon Aug 20 15:53:43 2021 -0300

    Minor bugfix in data processing module

Case Insensitive Searching

By default, ‘grep’ searches are case sensitive. However, you can easily perform a case insensitive search by adding the ‘-i’ option to your grep argument:

git log --grep="feature" -i

Using Regular Expressions

When you need to filter commits with more complex patterns, regular expressions come into play. Git grep supports the use of regex for matching, which can be utilized to perform more advanced searches.

git log --grep="bugfix\|hotfix"

The above command will match commit messages containing either ‘bugfix’ or ‘hotfix’.

Combining Filters

You may find that you need to apply multiple filtering criteria to your git log command. This is useful when you are searching for commit messages over specific authors, file patterns, times, etc. For instance, if you want to find commit messages with ‘error’ from a particular author, you can do so by combing the –grep and –author flags:

git log --grep="error" --author="Jane"

Inverting the Match

At times you might need to get all commits except those containing a certain message. For this, you can invert the match using the ‘–invert-grep’ flag.

git log --grep="typo" --invert-grep

This would show you commits that do not have the word ‘typo’ in their message.

Searching Across All Branches

To search commit messages across all branches in your repo, add the ‘–all’ flag. This command pulls out commits throughout your repository’s history, not just the history of the current branch.

git log --grep="refactor" --all

Limiting Log Output

If you are working with a large repository, git log’s output can be quite verbose. To make your output more concise, you can combine the grep filter with formatting options or constraints on the number of commits.

git log --grep="cleanup" --oneline --max-count=5

This command shows a one-line summary of the last 5 commits that have the word ‘cleanup’ in their messages.

Combining Git Log with other Unix Commands

For even more control and fine-grained filtering, you can pipe the output of git log into other Unix commands like ‘awk’, ‘sed’, or ‘cut’.

git log --grep="fix" | cut -c 1-40

This uses ‘cut’ to only display the first 40 characters of each line in your git log output, which normally contains the commit hash.

Using –format for Custom Output

If you only want to see certain information about each commit, you can specify that using git log’s –format option.

git log --grep="optimization" --format="%h %an %s"

This will display the abbreviated commit hash, author name and commit subject for all commits that include the term ‘optimization’ in their message, across all branches.

Conclusion

The flexibility of the git log command allows for a variety of strategies to filter and review commit messages. Armed with an understanding of how to apply these techniques, you can sift through commit histories efficiently and understand the narrative of your project’s development with greater clarity. Whether for debugging, code reviews, or auditing, mastering git log filtering methods is an important tool in any developer’s toolbelt.

Next Article: Explaining ‘git rebase –preserve-merges’ with examples

Previous Article: How to filter commits by author in Git log

Series: Git & GitHub Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide