Git Log Formatting: In-depth Analysis of Displaying Only the First Line of Commit Messages

Nov 22, 2025 · Programming · 8 views · 7.8

Keywords: Git log | Commit message formatting | Version control

Abstract: This article provides an in-depth exploration of Git log formatting mechanisms, focusing on how to display only the first line of commit messages. By analyzing the working principles of the git log --oneline command, it reveals Git's processing logic for commit message structures, including the definition standards for short descriptions and the critical role of empty lines. The article combines specific examples to detail the importance of standard commit message formats and offers comparative analysis of various formatting options to help developers better understand and utilize Git log functionality.

Analysis of Git Log Formatting Mechanism

In version control systems, Git provides powerful log viewing capabilities, with the formatting options of the git log command being particularly crucial. Users often need to customize log output formats, especially when they want to display only the first line of commit messages. This requirement stems from the need for quick browsing and concise presentation of commit history.

Core Problem and Solution

According to actual user cases, when attempting to use git log --pretty=short, the output does not display only the first line as expected. In-depth analysis shows that this is not an issue with the command itself but is closely related to the writing format of commit messages.

Git system processes commit messages following specific conventions: all content before the first empty line is considered the "short description." This means that if there is no empty line separation in the commit message, Git will recognize the entire message content as part of the short description. For example, for the following commit message:

Added some functionality.
+ Added print function in Foo class.
+ Added conversion from foo to baz.

Due to the lack of empty line separation, Git treats all three lines as the short description, causing the --pretty=short or --oneline options to output the complete content.

Standard Solution: git log --oneline

The most effective solution is to use the git log --oneline command. This command is an alias for git log --pretty=oneline --abbrev-commit, specifically designed to display commit history concisely.

The command output format is:

9bee8857 Write more code
831fdd6e Write some code Second line of message

This includes the shortened commit hash and the short description content. To ensure this command works properly, it is essential to follow Git's commit message conventions: insert an empty line before the detailed description.

Commit Message Format Standards

The correct commit message format should be:

Short description title

Detailed description content
- Feature point 1
- Feature point 2

This format ensures that Git can correctly identify the short description part, allowing formatting commands like --oneline to work as expected. This is a best practice widely adopted by the Git community, helping to maintain clear and readable commit history.

Alternative Formatting Options

In addition to --oneline, Git provides other formatting options:

These options provide different levels of detailed information, allowing developers to choose based on specific needs.

In-depth Technical Principle Analysis

Git internally uses specific parsing logic to process commit messages. When reading commit objects, Git scans the message content looking for the first empty line character \n\n. Content before the empty line is stored as the short description, while content after serves as the detailed description.

This design stems from Git's philosophy: commit messages should have a clear hierarchical structure. The short description serves as the commit's "title" and should be concise and clear; the detailed description can contain technical details, reasons for changes, and other supplementary information.

Practical Recommendations and Best Practices

Based on the above analysis, the following practical recommendations are proposed:

  1. Always insert an empty line between the short description and detailed description in commit messages
  2. Keep the short description within 50 characters for conciseness
  3. Use git log --oneline for daily quick history browsing
  4. Combine with --pretty=format for custom output when detailed information is needed

By following these conventions, developers can fully utilize Git's logging capabilities, improving the efficiency and quality of version control work.

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.