matthias_buehlmann
matthias_buehlmann

Reputation: 5061

git: how to get commit message (exactly) of commit?

How can I extract the commit message (and only/exactly the commit message) of a commit?

git show https://git-scm.com/docs/git-show doesn't seem to have an option for that

I could do git cat-file -p <commit_hash> and then search for the first \n\n and take everything after that until EOF, or I could do git log --format=%B -n 1 <commit_hash> but what is likely going to be forward compatible with future git versions? (of course, there's never a guarantee for that, but there's probably a 'best way' of doing this)

Upvotes: 2

Views: 2318

Answers (1)

Edward Thomson
Edward Thomson

Reputation: 78673

I would avoid trying to parse a file directly; using a git command is likely to provide a backward compatible API even if the underlying data format changes.

I would use avoid git log but instead use git show, which will let you examine a particular commit (instead of a range, which git log intends to do). It does, in fact, have an option for that, allowing you to specify custom formatting options.

To show only the commit message subject and body, use the %B format and turn off patch display.

git show --pretty=format:"%B" --no-patch

Upvotes: 5

Related Questions