Git: Show a commit message or subject

Hello there, let’s show commit messages.

To show only the subject of the latest commit (the first line of its message), use:

$ git show -s --format='%s'
Switch to computer-based internet

git show is the command to show a commit or other object. The options here:

Copy to clipboard

You might want to copy the subject in order to paste it into another tool, such as a GitHub PR. Do so by piping the output into your system’s “copy to clipboard” command. For example, on macOS, use pbcopy:

$ git show -s --format='%s' | pbcopy

Refer to my “copy to clipboard” post for the commands on other platforms.

Other commits

git show defaults to the latest commit, but you can also pass a branch name, tag, or other commit reference:

$ git show -s --format='%B' main
Press 'ANY' key

Other parts of the commit message

Show the full commit message with %B:

$ git show -s --format='%B'
Switch to computer-based internet

They have the internet on computers now!

Show only the body, which is everything after the subject, with %b:

$ git show -s --format='%b'
They have the internet on computers now!

And show the subject transformed into a format “suitable for a filename” with %f:

$ git show -s --format='%f'
Switch-to-computer-based-internet

Fin

Make your commits show your best self,

—Adam


Read my book Boost Your Git DX for many more Git lessons.


Subscribe via RSS, Twitter, Mastodon, or email:

One summary email a week, no spam, I pinky promise.

Related posts:

Tags: