How to output git log with the first line only?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Git, the first line of a commit message is the subject line, and that is usually the part people want in summaries, reports, or quick history views. You do not need to parse full commit messages manually to get it. Git already exposes the subject directly through --oneline or custom --format placeholders.
Fastest Option: --oneline
If you want the abbreviated hash plus the first line of each commit message, use:
This is shorthand for a compact format that prints:
- abbreviated commit hash
- subject line only
It is the most common answer when you want a readable one-line history.
Subject Only Without the Hash
If you want just the first line of the message and nothing else, use %s in a custom format:
%s means the commit subject. That is the canonical "first line only" field in Git formatting.
Example output:
This is better than post-processing full logs with head, awk, or sed because Git already knows which part of the commit message is the subject.
Include More Context When Needed
Sometimes you want the first line plus author or date. Use a richer format string:
This prints:
- abbreviated hash with
%h - formatted author date with
%ad - author name with
%an - subject with
%s
That is useful for release notes or changelog preparation where the full body would be too noisy.
The Difference Between %s and %B
Git format placeholders matter here:
- '
%sis the subject line' - '
%Bis the raw full commit message body plus subject'
So this:
will include multi-line commit messages, which is the opposite of what you want for "first line only" output.
If you only need the first line, %s is the correct placeholder.
Filter and Limit the Result
You can combine the same formatting with all the usual log filters.
Last five commit subjects:
Only one branch range:
Only commits by one author:
Only files in one path:
This is why custom formatting is more useful than exporting the full log and trimming it later.
Machine-Friendly Output
If another script consumes the output, avoid --oneline because it mixes two values into one human-focused line. Instead, choose an explicit separator:
Now the full hash and subject are easy to split reliably.
That matters if commit subjects can contain spaces, brackets, or punctuation. Structured formatting is more robust than relying on column spacing.
Aliases for Reuse
If you use this often, add an alias:
Then run:
That keeps the command short without losing the exact formatting you want.
Common Pitfalls
- Using
%Bwhen you only wanted the subject line. - Parsing full log output with shell tools instead of using Git's format placeholders.
- Assuming
--onelinemeans "message only" when it also includes the abbreviated hash. - Forgetting that commit messages can contain multi-line bodies even when the subject is short.
- Using human-formatted output in scripts when structured output would be safer.
Summary
- '
git log --onelinegives hash plus the first line of each commit.' - '
git log --format=%sgives only the first line, also called the subject.' - Use custom
--formatstrings when you want subject plus selected metadata. - Prefer Git's built-in placeholders over shell post-processing.
- For scripts, use explicit separators such as
%H|%sinstead of visually formatted output.

