NeuroAgent

How to Filter Git Log by User: Complete Guide

Learn how to filter git log by user with the --author flag. Discover advanced techniques, regex patterns, and formatting options to analyze specific contributors' commit history effectively.

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

You can view a git log of just one user’s commits by using the --author flag with the git log command. This option allows you to filter the commit history to show only commits made by a specific author, and it supports partial matches and regular expressions for flexible filtering.

Contents

Basic Author Filtering

The simplest way to filter git log by a specific user is using the --author option. This option accepts a string that can match the author’s name or email address.

bash
git log --author="John Doe"

This command will display only the commits made by “John Doe”. The --author option supports partial matches, so you can use:

bash
git log --author="John"  # Matches any author name containing "John"
git log --author="john.doe@example.com"  # Filter by email

According to the LabEx tutorial, the --author option is the most straightforward method for filtering commit logs by author and supports both plain text and regular expression patterns.

Advanced Author Filtering Techniques

For more sophisticated filtering, Git supports regular expressions with the --perl-regexp option. This allows for complex pattern matching and exclusion patterns.

Using Regular Expressions

To match authors using regex patterns:

bash
git log --author="^John" --perl-regexp  # Authors whose names start with "John"
git log --author="Doe$" --perl-regexp   # Authors whose names end with "Doe"

Excluding Specific Authors

You can exclude commits by specific authors using negative lookaheads:

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

As explained in Stephen Charles Weiss’s tutorial, this regex matches any author name that doesn’t start with “Stephen” or “John”.

Filtering Multiple Authors

To filter by a group of authors, you can combine patterns:

bash
git log --author="John\|Paul\|George\|Ringo"  # Using OR operator

Formatting and Customizing Output

Git’s --pretty=format option allows you to customize how commit information is displayed. This is particularly useful when filtering by author to create clean, readable output.

Common Format Specifiers

Here are some useful format specifiers for author-related information:

  • %an - Author name
  • %ae - Author email
  • %ad - Author date
  • %h - Abbreviated commit hash
  • %s - Subject (commit message)
  • %H - Full commit hash

Custom Format Examples

bash
# Clean, readable format with author and date
git log --author="John Doe" --pretty=format:"%h - %an, %ar : %s"

# Just commit hash and author name
git log --author="John" --pretty=format:"%h %an"

# With custom date formatting
git log --author="John" --pretty=format:"%h - %an - %ad" --date=format:"%Y-%m-%d"

According to the Git documentation on pretty formats, you can combine these specifiers to create virtually any output format you need.

Filtering by Committer vs Author

It’s important to understand the difference between author and committer:

  • Author: The person who originally wrote the code
  • Committer: The person who applied the commit to the repository

To filter by committer instead of author:

bash
git log --committer="John Doe"

As noted in the LabEx tutorial, the --committer option works similarly to --author but filters based on who actually made the commit rather than who wrote the code.

Combining Multiple Filters

You can combine the --author filter with other git log options to create more specific queries:

Date Filtering

bash
# Commits by John Doe in the last year
git log --author="John Doe" --since="1 year ago"

# Commits by John between specific dates
git log --author="John" --since="2023-01-01" --before="2024-01-01"

Message Content Filtering

bash
# Commits by John containing specific text in message
git log --author="John" --grep="bugfix"

Combining Author and Message Filters

When using multiple grep-like filters, you may need the --all-match option:

bash
git log --author="Hausmann" --grep="p4 depo" --all-match

As mentioned in the Git Reference, the --all-match option ensures that all conditions must be satisfied.

File-Specific Filtering

bash
# Commits by John that modified specific files
git log --author="John" -- path/to/file.js

Practical Examples

Here are some practical examples you might use in real development scenarios:

Example 1: Clean Author History

bash
# Show commits by John Doe in a clean format
git log --author="John Doe" --pretty=format:"%C(yellow)%h%Creset %Cgreen%ad%Creset %Cblue%an%Creset: %s" --date=format:"%Y-%m-%d"

Example 2: Exclude Merge Commits

bash
# Show non-merge commits by John Doe
git log --author="John" --no-merges

Example 3: Count Commits by Author

bash
# Count commits by John Doe
git log --author="John Doe" --oneline | wc -l

Example 4: Complex Regex Filtering

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

Example 5: GitHub-style Author Filtering

As mentioned in the Stack Overflow answer, you can also filter commits by author in GitHub’s web interface by appending ?author=github_handle to the commit view URL.


Sources

  1. Official Git Documentation - git-log
  2. How to filter Git commit logs by author - LabEx
  3. Filter git commits by author - Stephen Charles Weiss
  4. How to view Git log of one user’s commits - Stack Overflow
  5. How to view Git log of one user’s commits - GeeksforGeeks
  6. Git Reference - Inspect Commits
  7. Git pretty formats Documentation
  8. Filtering by Author Name - Adam Dymitruk
  9. Practical Git: Filter commit history with git log arguments - egghead.io
  10. Git - Viewing the Commit History - Pro Git

Conclusion

Filtering git log by user is a powerful technique for understanding individual contributions to a codebase. The --author flag provides flexible filtering capabilities that can be combined with other options for comprehensive analysis. Whether you need simple name matching, complex regex patterns, or custom output formatting, Git’s log filtering options give you the tools to efficiently examine commit history by specific users.

For most use cases, start with the basic git log --author="Name" command and gradually add more filtering options as needed. Remember to experiment with the --pretty=format option to create output that best serves your specific analysis requirements.