git
commit-message
version-control
git-log
developer-tools

Print commit message of a given commit in git

Master System Design with Codemia

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

Introduction

If you want the commit message for one specific Git commit, the simplest answer is to ask Git to format only that commit and print only the message field. The most practical commands are git show -s --format=%B and git log -1 --format=%B, both of which work well in scripts and shell pipelines.

Use git show for a Single Commit

The cleanest command is:

bash
git show -s --format=%B COMMIT_SHA

What each flag does:

  • '-s suppresses the diff output'
  • '--format=%B prints the raw full commit message'
  • 'COMMIT_SHA can be a full hash, short hash, tag, branch ref, or expressions such as HEAD~2'

Example:

bash
git show -s --format=%B HEAD

This prints the full message body for the current HEAD commit, including subject and any blank-line-separated body text.

git log Works Too

An equivalent form using git log is:

bash
git log -1 --format=%B COMMIT_SHA

This is useful if you are already thinking in terms of git log filters. For one commit, the result is effectively the same as git show -s --format=%B.

If you only want the one-line subject instead of the full message body, use %s instead of %B.

bash
git log -1 --format=%s HEAD

This distinction matters in scripts. %s gives the subject line only, while %B preserves the full message.

Common Format Placeholders

Git's pretty-format system gives you several message-related placeholders:

  • '%s for the subject line'
  • '%b for the body only'
  • '%B for the raw full message'

Example:

bash
git show -s --format='Subject: %s%nBody:%n%b' HEAD

That is useful when you want structured output for tooling or release-note generation.

When You Need the Message in a Script

In shell scripts, command substitution is usually enough.

bash
message=$(git show -s --format=%B 3a1f9c2)
printf 'Commit message:\n%s\n' "$message"

In Python, use subprocess.run and capture stdout.

python
1import subprocess
2
3result = subprocess.run(
4    ["git", "show", "-s", "--format=%B", "HEAD"],
5    check=True,
6    capture_output=True,
7    text=True,
8)
9
10print(result.stdout)

This is a reliable pattern for tooling that needs commit metadata.

Resolve the Commit Reference First if Needed

You do not need a full 40-character hash. Any revision expression that resolves to one commit works.

Examples:

bash
git show -s --format=%B main
git show -s --format=%B HEAD~3
git show -s --format=%B v1.2.0

Git resolves the revision expression before formatting the commit.

If the reference is ambiguous or invalid, Git will fail clearly. That is better than trying to parse git log output manually.

Plumbing Commands Exist, but Porcelain Is Usually Better

Git also has plumbing commands such as git cat-file -p that can expose raw commit objects.

bash
git cat-file -p HEAD

The output contains tree, parent, author, committer, a blank line, and then the commit message. It is powerful, but for the simple job of printing a message, git show or git log --format is easier and less error-prone.

Use plumbing only if you truly need the raw object layout.

Newlines and Trailing Output Matter

Commit messages may include multiple paragraphs and blank lines. If your script trims whitespace blindly, you may accidentally change the meaning or formatting of the message.

That is another reason %B is valuable. It preserves the raw message body instead of flattening it into one line.

When building shell pipelines, be careful with tools that collapse newlines or strip trailing whitespace unexpectedly.

Common Pitfalls

A common mistake is using plain git log COMMIT_SHA and then trying to parse the human-formatted output with grep or awk. Git already provides formatting flags; use them.

Another mistake is using %s when you actually need the full commit message body. That silently drops detail.

Developers also forget that git show prints a diff by default. Without -s, your script may receive much more output than expected.

Finally, avoid assuming only full hashes work. Short hashes and revision expressions are usually sufficient if they resolve uniquely.

Summary

  • Use git show -s --format=%B COMMIT_SHA to print the full message for one commit.
  • 'git log -1 --format=%B COMMIT_SHA is an equivalent alternative.'
  • '%s prints the subject only, while %B prints the full raw message.'
  • These commands are script-friendly and better than parsing human-oriented log output.
  • Use plumbing commands only when you need the raw commit object, not just the message.

Course illustration
Course illustration

All Rights Reserved.