Image of Lucian Ghinda writing for notes.ghinda.com
August 30th, 2024

Bash one line to display git commit message without indentation

I like to write explanatory git commit messages and if the PR/MR has one single commit, I use the commit message as the body of the Pull Request/Merge Request.

Using the simple git log command displays the commit message but has some padding/indentation:

Screenshot of git log command
git log output

There is a way to fix this by specifying the format: 

git log -1 --pretty='format:%s%n%n%b'

Where:
%s is the subject of the commit message (the first line)
%n is adding a new line (so I am adding two)
%b is the body of the commit message

Image with the output when executing a git command with format
git log -1 --pretty='format:%s%n%n%b'

You can make these bash aliases. 

alias .lastmsg="git log -1 --pretty='format:%s%n%n%b'"

And in case of MacOS you can directly copy the last message to clipboard: 

alias .cplastmsg="git log -1 --pretty='format:%s%n%n%b' | pbcopy"

Why the dot in the aliases?

I like to differentiate my own defined aliases from the any bash command. And it also makes sure there is no conflict or redefining existing aliases/Linux commands.Â