Sometimes I want a Git commit message without the leading indentation added by git show (for example, if I want to copy it and paste it somewhere else). This command gives me this output:
git show --no-patch --format='%B'
we can also optionally pass a reference to a Git commit (e.g. a branch, hash, or tag) just like we can with git show, e.g.
git show --no-patch --format='%B' HEAD~2
Alias
I have this command aliased to shnpfb (which is based on SHow No Patch Format B), though I typically type git shn<tab> so I can use tab completion and don’t have to think about all the letters.
You can add this alias to your global Git config file by running the following from the command line.
git config --global alias.shnpfb "show --no-patch --format='%B'"
Here is the commit where I added shnpfb to my Git config.
How it Works
The --no-patch flag excludes the actual code changes (a.k.a. the “patch”) from the output of git show
The --format flag for git show takes the same values as the Pretty Formats for Git Log, specifically we’re using %B which is
raw body (unwrapped subject and body)
Copying to the Clipboard on a Mac
On my Mac, I use the pbcopy command to copy the commit message to the clipboard.
e.g.
git show --no-patch --format='%B' | pbcopy
or with my alias
git shnpfb | pbcopy
Leave a Reply