How to filter commits by author in Git log

Updated: January 27, 2024 By: Guest Contributor Post a comment

Overview

If you’re working with Git in a multi-developer environment, understanding how to pinpoint specific changes can save you a lot of time. One essential skill is filtering commits by author in the git log, which enables you to quickly identify contributions from individual team members, a crucial part of code reviews, debugging, and project management. In this comprehensive guide, we will navigate through various commands from the simplest filters to more advanced combinations, using Git’s powerful querying capabilities.

The ‘git log’ Command

Before we dive into author-specific filtering, let’s cover the basics of the git log command. The git log command provides a chronological list of commits on the current branch. Without any arguments, git log displays all commits with their commit ID, author, date, and commit message.

git log

You can already see quite a bit of information, but the power of git log is in its ability to accept various options to tailor the output to your needs.

Basic Filtering by Author

The most straightforward way to filter the git log by author is using the --author option. This option takes a string which can be a plain text or a regular expression pattern that matches the name or email of the author.

git log --author="John Doe"

This command will show commits by the author with the name ‘John Doe’. For more fuzzy matching, you can use regular expressions:

git log --author="^John"

This regex will match any author whose name starts with ‘John’.

Searching by Email

In case multiple authors have the same name or you want to be more specific, you can search by an author’s email:

git log --author="[email protected]"

Similarly, you can use patterns to match part of an email address:

git log --author="@example.com$"

This matches any commits made by authors with an email ending in @example.com.

Combining Filters

One of the great things about git log is that you can combine filters. This allows you to narrow down the log entries to those that are most relevant. For example, you can filter by author and date:

git log --author="John Doe" --since="2023-01-01" --until="2023-01-31"

This command shows commits by ‘John Doe’ in January 2023.

Advanced Formatting

Another useful feature of git log is its formatting options. The --pretty flag can be used to customize the output to display exactly what you need. For instance, you could show just the commit hashes and messages for a specific author:

git log --author="John Doe" --pretty=format:"%h %s"

This will show a brief output with the short commit hash followed by the commit message.

Filter with Graphs and Stats

For a visual representation of the commit history, you can use the --graph option along with author filtering. This will display an ASCII graph of the commits on the left-hand side.

git log --author="John Doe" --graph

To also see the stats (the number of changes per file), combine --author with the --stat option:

git log --author="John Doe" --stat

This will not only show each commit made by the author but also include how many files were changed, as well as the number of lines added or removed in each file.

Casing and Exclusion

Sometimes case sensitivity might affect your results. Git log searches are case sensitive by default. However, you can control this with regular expressions:

git log --author="john doe" -i

The -i switch here tells Git to ignore case. Additionally, if you need to exclude commits by a particular author, you can do so using the ^ character in the regex:

git log --author="^(?!John Doe).*$" -i

This negates the match, so the log will show all commits by authors other than ‘John Doe’.

Log by Multiple Authors

For projects with large teams, you might want to see commits by more than one author. You can match multiple authors with a bit of regex wizardry:

git log --author="John Doe\|Jane Smith"

The pipe | character is used as the ‘OR’ operator in regular expressions, so this command matches commits by either ‘John Doe’ or ‘Jane Smith’.

Aliases for Simplicity

Typing these commands can get tiresome, especially if they’re commonly used. You can create a Git alias to run a complex log command with a simpler command:

git config --global alias.logs "log --pretty=format:'%h %s' --graph"

And now, you can just use git logs to execute your custom log viewing command.

Conclusion

Understanding how to filter the git log by author is a crucial tool in the developer’s toolkit. Whether you’re looking for a comprehensive history or a broad overview, these filters and formatting options enable a clearer view of individual contributions within projects, making collaboration and tracking easier to manage.