git
git log
version control
command line
programming tips

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

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

When working with Git, the need to focus on the contributions of a single user often arises. Whether for code reviews, performance assessments, or historical interest, viewing a Git log of just one user's commits can be invaluable. In this article, we will delve into how you can achieve this, providing you with a streamlined, user-specific view of your project's history.

Understanding Git Logs

Git logs are the history of changes to a repository. They include commit IDs, authorship details, timestamps, and commit messages. By default, a git log command displays these rich details for all commits in the current branch. To filter this log for a single user's contributions requires an understanding of a few key Git features.

Filtering Commits by Author

One of the simplest ways to view commits made by a specific user is to use the --author flag with git log. This flag allows you to query commits by the author's name or email address.

Basic Command

Here's an example command that fetches the log for a specific author:

bash
git log --author="Author Name"

You can replace Author Name with part or all of a user's name or their email address. Git will match this argument against commit authorship details.

Regular Expressions

If a developer has used different variations of their name or email, or if you only remember part of the author's credentials, you can use regular expressions:

bash
git log --author="^John.*Doe$"

This example would match any committer whose name starts with "John" and ends with "Doe". Regular expressions in Git follow standard PCRE (Perl Compatible Regular Expressions) syntax.

Enhancing the Log Output

While filtering for a single author's commits, you might want to customize the log output to include or exclude specific information.

Customized Log Format

You can use the --pretty option to customize the log's format:

bash
git log --author="Author Name" --pretty=format:"%h - %an, %ar : %s"
  • %h: Abbreviated commit hash.
  • %an: Author name.
  • %ar: Relative timestamp.
  • %s: Commit message.

Additional Filters

It is possible to refine the output further by filtering for specific files or date ranges:

  • Date Range:
bash
  git log --author="Author Name" --since="2023-01-01" --until="2023-12-31"
  • Specific Files:
bash
  git log --author="Author Name" -- path/to/file

Viewing Changes per Commit

If you wish to not only see which commits a user made but also want to inspect the changes introduced in those commits, you can add the -p option to show the patch (diff) of each commit:

bash
git log --author="Author Name" -p

Summary Table

Here's a quick summary of key commands and their purpose:

CommandPurpose
git log --author="Author Name"List commits from a specific user.
git log --author="^John.*Doe$"Use a regex to find commits from authors matching a pattern.
git log --author="Author Name" -pDisplay changes in each commit by a specific author.
git log --author="Author Name" --since="date"View commits from a specific user after a certain date.
git log --author="Author Name" -- path/to/fileShow commits by a user for a specific file.
git log --author="Author Name" --pretty=format:"<format>"Customize log entry format for readability.

Conclusion

Being able to filter and view Git logs based on specific user contributions is a powerful feature for developers, team leads, and project managers. By employing Git's filtering options and understanding formatting commands, you can obtain a clear record of individual contributions. These skills enhance your ability to audit code, facilitate focused reviews, and gain insights into a project's development history.


Course illustration
Course illustration

All Rights Reserved.