376

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

Mark Probst
  • 7,107
  • 7
  • 40
  • 42
  • 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 ` – waldyrious Jan 13 '17 at 12:34
  • Where should we add the line to print the commit message in server machine? I meant which hook file? –  Aug 30 '17 at 12:17
  • 3
    What do you mean by "plumbing" command? – Bryan Ash Sep 18 '19 at 21:08
  • 2
    @BryanAsh https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain – CervEd May 01 '21 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.` – questionto42 May 20 '22 at 18:07

8 Answers8

457

It's not "plumbing", but it'll do exactly what you want:

$ 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.

mipadi
  • 398,885
  • 90
  • 523
  • 479
194

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

git show -s --format=%B SHA1
CharlesB
  • 86,532
  • 28
  • 194
  • 218
  • 16
    And if you just want the first line ("subject"), use `%s` instead of `%B`. – ntc2 Sep 25 '14 at 17:00
  • 24
    (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 Oct 26 '15 at 10:58
  • @Rich what do you mean by that? – CervEd May 01 '21 at 11:17
  • @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)`. – Rich May 04 '21 at 11:00
  • @Rich `git show` is a lot slower than `git rev-list --max-count 1` – CervEd May 04 '21 at 11:22
  • @CervEd:You're right; that's really weird. I don't know why that would be. In my mental model of things, rev-list ought to be slower as it is doing the same work on each element of a list that "show" is doing on a single named commit. Either I misunderstand something or rev-list is optimised in some way that show isn't (but could be, for this case at least). – Rich May 05 '21 at 12:55
  • 2
    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 – CervEd May 05 '21 at 13:24
  • The fact that `git show` needs `--no-pager` kind of shows you it's not a plumbing command. – user541686 Dec 01 '22 at 06:08
23

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.)

bstpierre
  • 30,042
  • 15
  • 70
  • 103
  • 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 https://stackoverflow.com/questions/7005513/pass-an-argument-to-a-git-alias-command You just run this git lsum 5 – kuklei Jun 18 '20 at 11:11
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
Justin
  • 84,773
  • 49
  • 224
  • 367
Harshniket Seta
  • 654
  • 7
  • 7
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

nos
  • 19,875
  • 27
  • 98
  • 134
  • 2
    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. – Canonical Chris Nov 26 '19 at 14:18
10

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.

CervEd
  • 3,306
  • 28
  • 25
  • This won't work for merge commits though that have two parents: you'll have to use `tail +3`. – Leponzo May 07 '21 at 19:32
  • @leponzo, bummer. Maybe it's better to use some kind of sed magic to get rid of the shas reliably – CervEd May 08 '21 at 05:09
  • 1
    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'` (https://stackoverflow.com/a/68514358/10807837) – T.D May 24 '23 at 20:43
3

I use shortlog for this:

$ git shortlog master..
Username (3):
      Write something
      Add something
      Bump to 1.3.8 
mja
  • 1,273
  • 1
  • 20
  • 22
2

To Get my Last Commit Message alone in git

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