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:
What each flag does:
- '
-ssuppresses the diff output' - '
--format=%Bprints the raw full commit message' - '
COMMIT_SHAcan be a full hash, short hash, tag, branch ref, or expressions such asHEAD~2'
Example:
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:
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.
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:
- '
%sfor the subject line' - '
%bfor the body only' - '
%Bfor the raw full message'
Example:
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.
In Python, use subprocess.run and capture 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:
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.
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_SHAto print the full message for one commit. - '
git log -1 --format=%B COMMIT_SHAis an equivalent alternative.' - '
%sprints the subject only, while%Bprints 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.

