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?
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
- Filtering by Email Address
- Using Regular Expressions
- Filtering Multiple Authors
- Excluding Specific Authors
- Additional Filtering Options
- Practical Examples
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.
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:
- It does not get confused by authors using various names on various computers
- It prevents the issue that filtering by “authorname” will also show results for “authorname2” etc.
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.
# 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:
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:
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:
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.
# 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:
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:
# 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
git log --author="John Doe"
Example 2: Viewing by Email
git log --author="john.doe@example.com"
Example 3: Date Range Filtering
git log --author="John Doe" --since="2023-01-01" --until="2023-12-31"
Example 4: Multiple Authors
git log --author="John|Jane"
Example 5: Excluding Authors
git log --author='^(?!Stephen|John).*$' --perl-regexp
Example 6: Combined with Other Options
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
- How to filter Git commit logs by author | LabEx
- How to use the --author option with git log command | LabEx
- How can I view a git log of just one user’s commits? - Stack Overflow
- Git - git-log Documentation
- Git - Filtering the Commit History - GeeksforGeeks
- How Can I View a Git Log of Just One User’s Commits? | Better Stack Community
- How to filter commits by author in Git log - Sling Academy
- Filtering by Author Name - Adam Dymitruk
- GIT: filter log by group of authors - Stack Overflow
- filter git commits by author | /code-comments/
- Other - “git log” does not work for multiple author filtering | The FreeBSD Forums
- Git - git-shortlog Documentation
- 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-regexpfor 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.