Better way of getting a GIT commit message by short hash?

I like having the important stuff dumped into a single line... Here's what I use, built off other answers on this page:

git_log_for_commit.sh

IT=$(git log -1 --pretty=format:"%an, %s, %b, %ai"  $*)
echo "$IT"

output

jdoe, WORK1766032 - Added templating engine, WIP, 2013-08-15 14:25:59 +0000

Depending on how much of the commit message you actually want, there are several pretty-format specifiers that you can use:

      ·  %s: subject
      ·  %f: sanitized subject line, suitable for a filename
      ·  %b: body
      ·  %B: raw body (unwrapped subject and body)

So something like git log -1 --pretty=format:%b <hash>, or use one of the other specifiers (I think %s is probably closer to what you are doing now). The -1 limits git log to just the one commit, rather than walking the history tree.


An even shorter answer than is listed here is

git log --pretty=oneline {your_hash} | grep {your_hash}


git log takes (among other things):

  • -n num to limit the number of commits shown: choose 1 (and if num is 9 or less you can just write -num, hence, -1, for short)
  • --pretty=format:string with directives to change the log output format. The %s directive gets the commit "subject", which is what you also get with oneline.

Hence: git log -n 1 --pretty=format:%s $hash (or git log -1 --pretty=format:%s) will do the trick here.

For a complete list of format directives, see the git log documentation, under "PRETTY FORMATS" (about halfway down).