Viewing Comments and Times of Last N Commits in Git: Efficient Command-Line Methods and Custom Configurations

Dec 07, 2025 · Programming · 14 views · 7.8

Keywords: Git | commit history | command-line operations

Abstract: This article explores methods to view comments and times of a user's last N commits in Git. Based on a high-scoring Stack Overflow answer, it first introduces basic operations using the git log command with --author and -n parameters to filter commits by a specific author. It then details the advantages of the --oneline parameter for simplified output, illustrated with code examples. Further, the article extends to advanced techniques for customizing git log format, including using the --pretty=format parameter to tailor output and creating aliases to enhance daily workflow efficiency. Finally, through practical terminal output examples, it validates the effectiveness and visual appeal of these methods, providing a comprehensive, actionable solution for developers to manage commit histories.

Basic Commands for Viewing Git Commit History

In the Git version control system, reviewing commit history is a common task in daily development. Users often need to quickly access comments and times of their last N commits to track code changes or conduct code reviews. Git provides the powerful git log command for this purpose, but default output may include excessive redundant information, such as commits from all users or detailed file change lists. Therefore, mastering how to efficiently filter and format output is crucial.

Filtering Commits by Specific User with --author Parameter

To view only commits from a specific user, the --author parameter can be used. This parameter accepts a string value, typically the user's name or email address, to match the commit author. For example, if the username is "Salvador", the following command shows their last 5 commits:

git log -n 5 --author=Salvador

This command outputs complete commit information, including commit hash, author, date, comment, and related file changes. The -n 5 parameter specifies displaying the last 5 records, while --author=Salvador ensures only commits by that author are shown. This method is straightforward but may produce verbose output unsuitable for quick browsing.

Simplifying Output: Application of --oneline Parameter

To improve readability, Git offers the --oneline parameter, which compresses each commit record into a single line. Combined with --author and -n parameters, it creates a concise viewing command. For instance:

git log --oneline -n 5 --author=Salvador

After executing this command, the output might resemble:

a1b2c3d Fix bug in login module
e4f5g6h Add new feature for user profile
i7j8k9l Update documentation
m0n1o2p Refactor code structure
q3r4s5t Initial commit

Each line includes the first 7 characters of the commit hash and the commit comment, enabling users to quickly identify key changes. This format is particularly useful for rapid review of commit history or generating concise reports.

Advanced Customization: Using --pretty=format Parameter

For more complex output needs, Git allows customization through the --pretty=format parameter. This parameter accepts a format string that specifies which fields to output and how to display them. For example, the following command demonstrates a custom format including commit hash, branch information, comment, relative time, and author:

git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

In this example, the format string uses color codes (e.g., %Cred for red) and placeholders (e.g., %h for abbreviated commit hash, %s for comment, %cr for relative time, %an for author name). The output is displayed in a colored, graphical manner to enhance readability. For instance, in a terminal, it might show:

* a1b2c3d - (HEAD -> main) Fix bug in login module (2 hours ago) <Salvador>
* e4f5g6h - Add new feature for user profile (1 day ago) <Salvador>

This approach not only provides rich information but also improves user experience through visual elements like colors and graphs.

Creating Aliases to Boost Workflow Efficiency

To simplify daily operations, users can create aliases for frequently used git log commands. In shell configuration files (e.g., ~/.zshrc or ~/.bashrc), an alias can be defined as follows:

alias glog="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

After definition, users can simply type glog -n 5 in the terminal to view customized output of the last 5 commits. Additionally, the alias still supports other parameters, such as --author, for further filtering. For example:

glog -n 5 --author=Salvador

This outputs a colored, graphical record of Salvador's last 5 commits. The alias mechanism significantly reduces the burden of typing complex commands, enhancing development efficiency.

Practical Applications and Output Examples

Using the methods described, users can flexibly view commit records based on different scenarios. For instance, in team collaboration, the --author parameter allows quick filtering of personal contributions; during code reviews, the --oneline format facilitates rapid browsing of change summaries; and custom formats are suitable for generating detailed reports or presentations. A typical terminal output example is:

* a1b2c3d - (HEAD -> main) Fix bug in login module (2 hours ago) <Salvador>
* e4f5g6h - Add new feature for user profile (1 day ago) <Salvador>
* i7j8k9l - Update documentation (3 days ago) <Salvador>
* m0n1o2p - Refactor code structure (1 week ago) <Salvador>
* q3r4s5t - Initial commit (2 weeks ago) <Salvador>

This output includes core information (e.g., comments and times) while enhancing readability through colors and graphical elements, helping users intuitively understand commit history.

Conclusion and Best Practices

Viewing comments and times of the last N commits in Git is a fundamental yet important operation. By mastering parameter combinations of the git log command, such as --author, -n, --oneline, and --pretty=format, users can efficiently filter and format output. Furthermore, creating aliases can streamline workflows. It is recommended to choose appropriate methods based on actual needs: use --oneline for quick views, custom formats for detailed analysis, and aliases for repetitive tasks. These techniques not only boost individual productivity but also facilitate code management and review processes in team collaborations.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.