git
commit
version control
commit message
git commands

Display last git commit comment

Master System Design with Codemia

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

Introduction

Reading the last commit message is simple in Git, but the right command depends on whether you need only the subject or the full multi line body. Picking explicit format specifiers avoids fragile parsing and makes scripts reliable. This guide covers direct terminal usage and automation friendly patterns.

Core Sections

Getting Subject and Full Message Text

Git stores a subject line and optional body for each commit. Use %s when you only need the subject and %B when you need the whole message block exactly as written.

bash
1# Subject only
2git log -1 --pretty=%s
3
4# Full message, including body
5git log -1 --pretty=%B
6
7# Alternative command with no patch output
8git show -s --format=%B HEAD

Use subject output for compact notifications. Use full body output for release notes or changelog generation where extra details matter.

If you need the last message from another repository path, add -C:

bash
git -C /Users/markqian/codemia-content log -1 --pretty=%B

This avoids directory changes in scripts that operate on many repositories.

Structured Output for Scripts and CI

Automation usually needs more than the message. Include hash, author, and date in a delimiter based format so parsing is deterministic.

bash
git log -1 --pretty='format:%H|%an|%ae|%ad|%s' --date=iso-strict

For safer parsing, use a low probability separator character:

bash
git log -1 --pretty='format:%H%x1f%an%x1f%ae%x1f%s'

In shell scripts, capture values with care:

bash
1sha=$(git rev-parse --short HEAD)
2subject=$(git log -1 --pretty=%s)
3body=$(git log -1 --pretty=%B)
4
5printf 'sha=%s\nsubject=%s\nbody=%s\n' "$sha" "$subject" "$body"

This keeps formatting stable across environments and avoids scraping default git log text, which is not designed for machine parsing.

Handling Edge Cases in Real Repositories

New repositories may have no commits. Robust scripts should check HEAD before trying to print messages.

bash
1if git rev-parse --verify HEAD >/dev/null 2>&1; then
2  git log -1 --pretty=%B
3else
4  echo "No commits available"
5fi

Detached HEAD still has a current commit, so message lookup works. Branch name lookup may fail, so treat that as a separate optional step.

For merge workflows, sometimes the latest commit is a merge commit with a generated message. If you want the most recent non merge subject, filter merges out:

bash
git log --no-merges -1 --pretty=%s

For signed commits, message extraction commands stay the same. Verification can be added as an extra check:

bash
git log -1 --show-signature --pretty=%s

Helpful Aliases for Teams

Standard aliases reduce friction and keep documentation simple. Add these once in your global config:

bash
git config --global alias.lastsub 'log -1 --pretty=%s'
git config --global alias.lastmsg 'log -1 --pretty=%B'
git config --global alias.lastmeta "log -1 --pretty=format:'%H|%an|%ad|%s' --date=iso-strict"

Now team members can run git lastsub, git lastmsg, and git lastmeta consistently across projects.

If your team requires ticket IDs, validate the latest subject before pushing:

bash
1subject=$(git log -1 --pretty=%s)
2case "$subject" in
3  ABC-*) echo "subject is valid" ;;
4  *) echo "missing ticket prefix"; exit 1 ;;
5esac

This protects release metadata quality without changing individual contributor workflows.

Common Pitfalls

  • Using git show without -s and accidentally capturing patch output.
  • Expecting %s to include body paragraphs.
  • Parsing default git log output with brittle text matching.
  • Forgetting empty repository checks in CI scripts.
  • Choosing separators that can appear in commit text and break parsing.

Summary

  • Use %s for subject and %B for full commit message text.
  • Prefer explicit format strings for predictable script output.
  • Add -C for repository path targeting in automation.
  • Guard against repositories with no commits.
  • Standard aliases improve consistency across teams and pipelines.

Course illustration
Course illustration

All Rights Reserved.