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

6

40

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?

Erickericka answered 5/11, 2013 at 20:33 Comment(2)
You're already quite close, just limit output to 1 commit and use a different --pretty: git log -n 1 --pretty=format:%s $hashGerick
@Gerick plus one to your comment, and +1 and accepted if you want to post this as an answer.Erickericka
G
52

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

Gerick answered 5/11, 2013 at 21:26 Comment(0)
O
12

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.

Overtime answered 5/11, 2013 at 21:2 Comment(0)
P
8

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
Parfleche answered 19/7, 2017 at 17:51 Comment(0)
V
3

An even shorter answer than is listed here is

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

Valery answered 13/2, 2019 at 21:3 Comment(1)
There is also git log --oneline but the hash is short instead of the full version.Revere
P
2

Try this:

git show --pretty=format:"%s" -s "${hash}"
Plasticity answered 11/5, 2023 at 15:6 Comment(0)
D
1

This may short it some

git log --pretty=oneline ${hash} | awk '$0~var {print $2}' var="${hash}"
Dianemarie answered 5/11, 2013 at 20:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.