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

ehime picture ehime · Nov 5, 2013 · Viewed 16.7k times · Source

I am currently getting my commit message for a certain commit hash by using this below:

hash='b55da97'
git log --pretty=oneline ${hash} | grep "${hash}" | awk '{ print $2 }'

These seems extremely inefficient though. Is there a smarter or cheaper way to do this, or am I stuck with grepping and awking?

Answer

torek picture torek · Nov 5, 2013

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