Git: Show a commit message or subject
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:
-s
suppresses showing the diff, whichgit show
does by default.--format='%s'
shows the%s
format code to show the subject.
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
Read my book Boost Your Git DX for many more Git lessons.
One summary email a week, no spam, I pinky promise.
Related posts:
- Git: Show the initial (root) commit
- Git: Output the top-level directory of the current repository
- Git: Output just the current branch name
Tags: git