406

I need a plumbing command to print the commit message of one given commit - nothing more, nothing less.

5
  • 7
    If anyone comes here looking for a way to print a single-line commit message but doesn't care about the hash appearing at the beginning, the following works: git show -s --oneline <commit> Commented Jan 13, 2017 at 12:34
  • Where should we add the line to print the commit message in server machine? I meant which hook file? Commented Aug 30, 2017 at 12:17
  • 3
    What do you mean by "plumbing" command? Commented Sep 18, 2019 at 21:08
  • 2
    @BryanAsh git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain Commented May 1, 2021 at 11:11
  • 3
    @BryanAsh from that link: Plumbing and Porcelain This book covers primarily how to use Git with 30 or so subcommands such as checkout, branch, remote, and so on. But because Git was initially a toolkit for a version control system rather than a full user-friendly VCS, it has a number of subcommands that do low-level work and were designed to be chained together UNIX-style or called from scripts. These commands are generally referred to as Git’s “plumbing” commands, while the more user-friendly commands are called “porcelain” commands. Commented May 20, 2022 at 18:07

8 Answers 8

503

It's not "plumbing", but it'll do exactly what you want (%B represents the whole commit message):

$ git log --format=%B -n 1 <commit>

If you absolutely need a "plumbing" command (not sure why that's a requirement), you can use rev-list:

$ git rev-list --format=%B --max-count=1 <commit>

Although rev-list will also print out the commit sha (on the first line) in addition to the commit message.

Sign up to request clarification or add additional context in comments.

9 Comments

%B is a correct specifier (at least, in Git 1.7.2, not sure when it was added).
%B was added sometime after 1.7.1 (probably in 1.7.1.1).
Indeed it is - Git 1.7.2 apparently. Muchas gracias!
I prefer %s instead of %B because it prints only the commit message subject, not the body (expanded description) if one is supplied
@AndrewSpencer, no extensive testing but git log appears slower
|
207

git show is more a plumbing command than git log, and has the same formatting options:

git show -s --format=%B SHA1

8 Comments

And if you just want the first line ("subject"), use %s instead of %B.
(I prefer this to the accepted "log" or "rev-list" answer, as it's a single-commit operation, rather than a list restricted to 1 entry.)
@Rich what do you mean by that?
@CervEd - I mean that the "log" and "rev-list" commands operate on lists of commits, and the other answers here restrict the list to length 1. It's an unnecessary level of indirection and complicates things. The "log" op in git is kind of like doing "let xs = [123]; for (x in xs) { show(x) }", but it's simpler and quicker to just do show(123).
I think you're making the assumption that because rev-list has a max-count option and show doesn't that rev-list is iterating over the whole tree or doing something computationally intensive. But you can't make such assumptions on the internal workings based on the public API. git show is a porcelain command, without having dug deep, I'm assuming it's using the exact same mechanics as rev-list as well as a whole bunch of other user-friendly operations that slow it down. Porcelain commands are not optimized for speed
|
24

Not plumbing, but I have these in my .gitconfig:

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

That's "last summary" and "last message". You can provide a commit to get the summary or message of that commit. (I'm using 1.7.0.5 so don't have %B.)

1 Comment

You can pass -n argument as a parameter. So instead of only return last commit you can make it to return last 5 commits. Here the change necessary lsum = "!f() { git log -n $1 --pretty=format:'%s'; }; f" lmsg = "!f() { git log -n $1 --pretty=format:'%s%n%n%b'; }; f" Found it here stackoverflow.com/questions/7005513/… You just run this git lsum 5
19

This will give you a very compact list of all messages for any specified time.

git log --since=1/11/2011 --until=28/11/2011 --no-merges --format=%B > CHANGELOG.TXT

Comments

18

I started to use

git show-branch --no-name <hash>

It seems to be faster than

git show -s --format=%s <hash>

Both give the same result

I actually wrote a small tool to see the status of all my repos. You can find it on github.

enter image description here

1 Comment

That shows the first line only (%s), not the whole commit message ("nothing more, nothing less", as the asker wrote). If you have a way of doing show-branch with %B, then that would be helpful.
13

Print commit message using git-rev-list

git-rev-list is the plumbing command that let's you print the message of a commit.

Use it like this.

git rev-list --max-count=1 --no-commit-header --format=%B <commit>
  • --max-count=1: we're just interested in one commit
  • --no-commit-header: Don't show the default commit header
  • --format=%B: show message (subject %s + %n%n + body %b)
  • <commit>: a sha, HEAD, branch-name, tag-name, branch1...branch2 etc.

It's a lot faster than git log or git show.

3 Comments

This won't work for merge commits though that have two parents: you'll have to use tail +3.
@leponzo, bummer. Maybe it's better to use some kind of sed magic to get rid of the shas reliably
newer versions of git (2.33+) have added a --no-commit-header option to rev-list. If you need use sed, you can use sed '/^commit [0-9a-f]\{40\}$/d' (stackoverflow.com/a/68514358/10807837)
4

I use shortlog for this:

$ git shortlog master..
Username (3):
      Write something
      Add something
      Bump to 1.3.8 

Comments

3

To Get my Last Commit Message alone in git

git log --format=%B -n 1 $(git log -1 --pretty=format:"%h") | cat -

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.