NeuroAgent

Git Log Author Filtering: Ultimate Guide

Learn how to filter git log by author name or email. Complete guide with regex patterns, multiple author filtering, and practical examples for analyzing code contributions.

Question

How can I view a git log of just one user’s commits?

When using git log, how can I filter by user so that I see only commits from that user?

NeuroAgent

To view a git log of just one user’s commits, use the --author option with the git log command. You can filter by the author’s name or email address using git log --author="John Doe" or git log --author="john.doe@example.com" respectively. This powerful filtering capability allows you to focus on specific contributions and analyze individual work patterns within your repository.

Contents

Basic Author Filtering

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 of the author.

bash
git log --author="John Doe"

This command will show all commits where the author’s name contains “John Doe”. The filtering is case-insensitive by default, so it will match variations in capitalization. According to the official git documentation, this option limits the commits output to ones with author header lines that match the specified pattern.

For looking for commits by “Adam Dymitruk” it’s easier to just type git log --author="Adam" or use the last name if there are more contributors with the same first name source.

Filtering by Email Address

Filtering by email address is often more reliable than filtering by name because:

  1. It does not get confused by authors using various names on various computers
  2. It prevents the issue that filtering by “authorname” will also show results for “authorname2” etc.
bash
git log --author="john.doe@example.com"

As noted in the Stack Overflow discussion, filtering by an email address has these advantages source.

The email address filtering is particularly useful in team environments where contributors might use different names but have consistent email addresses across commits.

Using Regular Expressions

Git’s --author option supports regular expressions for more complex filtering patterns. You can use regex to match partial names, multiple patterns, or specific formats.

bash
# Match authors with "John" in their name
git log --author="John"

# Match authors with "Doe" in their name
git log --author="Doe"

# Use extended regex for more complex patterns
git log --author="John|Jane" --grep-extended-regexp

According to the git documentation, the --author option uses regular expressions for matching, and with more than one --author=<pattern>, commits whose author matches any of the patterns are chosen.

For enhanced regex capabilities, you might need to enable extended regex:

bash
git log --author="Andrew|Stephen" --since="2016-09-20"

Some systems may require setting git config --global grep.extendedRegexp true for extended regex to work properly source.

Filtering Multiple Authors

You can easily match on multiple authors as regex is the underlying mechanism for this filter. To list commits by Jonathan or Adam, you can do this:

bash
git log --author="Jonathan|Adam"

The Git log can take multiple options so you can combine them for your needs. With more than one --author=<pattern>, commits whose author matches any of the <pattern> are chosen source.

For more complex multiple author filtering, you can use:

bash
git log --author="John Doe" --author="Jane Smith"

This will show commits from either author.

Excluding Specific Authors

Sometimes you need to exclude commits from specific authors. You can achieve this using regular expressions in combination with the --perl-regexp switch and negative lookaheads.

bash
# Exclude commits by Stephen or John
git log --author='^(?!Stephen|John).*$' --perl-regexp

As explained in the /code-comments/ blog, you can exclude commits by those authors using a regular expression in combination with the --perl-regexp (to get negative lookaheads) source.

Another approach shown in Stack Overflow is:

bash
git log --perl-regexp --author='(?<!(john|paul|ringo))@'

This uses a negative lookbehind to exclude certain authors.

Additional Filtering Options

You can combine author filtering with other git log options for more precise results:

bash
# Filter by author and date range
git log --author="John Doe" --since="2023-01-01" --until="2023-12-31"

# Filter by author and show only commit hashes
git log --author="John Doe" --pretty=format:"%H"

# Filter by author and show stats
git log --author="John Doe" --stat

# Filter by author and limit number of commits
git log --author="John Doe" --oneline -10

The git log command is highly customizable and can be combined with various formatting options to display the information in the most useful way for your needs.

Practical Examples

Here are some practical examples of using author filtering in real-world scenarios:

Example 1: Viewing by Name

bash
git log --author="John Doe"

Example 2: Viewing by Email

bash
git log --author="john.doe@example.com"

Example 3: Date Range Filtering

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

Example 4: Multiple Authors

bash
git log --author="John|Jane"

Example 5: Excluding Authors

bash
git log --author='^(?!Stephen|John).*$' --perl-regexp

Example 6: Combined with Other Options

bash
git log --author="John Doe" --oneline --graph --decorate

These examples demonstrate the flexibility of git log author filtering and how it can be adapted to various workflow needs.

Sources

  1. How to filter Git commit logs by author | LabEx
  2. How to use the --author option with git log command | LabEx
  3. How can I view a git log of just one user’s commits? - Stack Overflow
  4. Git - git-log Documentation
  5. Git - Filtering the Commit History - GeeksforGeeks
  6. How Can I View a Git Log of Just One User’s Commits? | Better Stack Community
  7. How to filter commits by author in Git log - Sling Academy
  8. Filtering by Author Name - Adam Dymitruk
  9. GIT: filter log by group of authors - Stack Overflow
  10. filter git commits by author | /code-comments/
  11. Other - “git log” does not work for multiple author filtering | The FreeBSD Forums
  12. Git - git-shortlog Documentation
  13. Useful tricks you might not know about git log

Conclusion

Filtering git log by author is a powerful technique that helps you focus on specific contributions within your repository. The key takeaways are:

  • Use git log --author="name" to filter by author name
  • Prefer email filtering (git log --author="email@example.com") for more reliable results
  • Leverage regular expressions for complex filtering patterns
  • Combine author filtering with other git log options for comprehensive analysis
  • Use --perl-regexp for advanced regex patterns including exclusions

By mastering these filtering techniques, you can efficiently analyze code contributions, track individual productivity, and gain valuable insights into your team’s development patterns. Experiment with different filtering approaches to find the most useful way to examine your project’s history.