git log
command line
git tips
version control
git tricks

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:

bash
git log --oneline

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:

bash
git log --format=%s

%s means the commit subject. That is the canonical "first line only" field in Git formatting.

Example output:

text
Fix login validation
Add health check endpoint
Refactor payment service

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:

bash
git log --format="%h %ad %an %s" --date=short

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:

  • '%s is the subject line'
  • '%B is the raw full commit message body plus subject'

So this:

bash
git log --format=%B

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:

bash
git log -5 --format=%s

Only one branch range:

bash
git log main..feature-branch --format="%h %s"

Only commits by one author:

bash
git log --author="Alice" --format=%s

Only files in one path:

bash
git log --format="%h %s" -- src/api/

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:

bash
git log --format="%H|%s"

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:

bash
git config --global alias.subjects "log --format=%s"

Then run:

bash
git subjects

That keeps the command short without losing the exact formatting you want.

Common Pitfalls

  • Using %B when you only wanted the subject line.
  • Parsing full log output with shell tools instead of using Git's format placeholders.
  • Assuming --oneline means "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 --oneline gives hash plus the first line of each commit.'
  • 'git log --format=%s gives only the first line, also called the subject.'
  • Use custom --format strings 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|%s instead of visually formatted output.

Course illustration
Course illustration

All Rights Reserved.