Pergunta

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?

Foi útil?

Solução

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

Outras dicas

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.

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

An even shorter answer than is listed here is

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

This may short it some

git log --pretty=oneline ${hash} | awk '$0~var {print $2}' var="${hash}"
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top